2

There is challenge on code wars to calculate how many times you have to multiply a Long's individual integers against each other before it becomes a single digit. for example

39 -> 3 * 9 = 27 -> 2 * 7 = 14 -> 1 * 4 = 4 // answer is 3

Here is one of the posted solutions -

class Persist {
  public static int persistence(long n) {
    int times = 0;
    while (n >= 10) {
      n = Long.toString(n).chars().reduce(1, (r, i) -> r * (i - '0'));
      times++;
    }
    return times;
  }
}

I am very confused by the "(i - '0')" portion of the code. I just learned yesterday that Java's chars() method returns an IntStream which represent the chars so immediately using the reduce makes sense to me. But then it substracts a character which throws me off because it seems to apply that it is working with chars, but then how are they being multiplied together?

I copied the above code and then deleted the character subtraction so that is was a simple reduce statement that I understood, aka

n = Long.toString(n).chars().reduce(1, (r, i) -> r * i);

and then ran the debugger. The very first loop calculated 3 * 9 as 2907. Where does that answer come from? My best guess is that it has to do with character encoding but then why does subtracting the char '0' fix it?

2
  • 3
    '0' is decimal 48, '1' is decimal 49. So '0' - '0' equals 0, '1' - '0' equals 1 and so on. It basically turns the character into its digit counterpart
    – QBrute
    Feb 3, 2021 at 20:40
  • 1
    that should be easy to find out, no? System.out.println((int)'0');
    – Eugene
    Feb 3, 2021 at 22:29

1 Answer 1

5

A char is just a number in the range 0 to 0xFFFF with a special meaning when printing or concatenating to a string.

You can easily write

int i = 'A';

but also

char c = 65;

Both variables refer to the same number, hence,

System.out.println(i == c);

will print true but when you execute

System.out.println(i);
System.out.println(c);

the different variables types will cause selecting different methods with a different interpretation of the values.

You can use char values for calculations and always assign them to int variables, as their value range fits into the int value range, so it’s possible to process a sequence of char values with an IntStream and save the creation of another specialized Stream class. You only have to care to interpret the result values correctly.

Speaking of encodings, char is defined as an UTF-16 unit, so if you want to give it a name, that’s the right one.

For the example of your question, it’s crucial to know that the chars '0' to '9' have adjacent values in the UTF-16 encoding (it applies to all Unicode encodings), so not only does subtracting '0' from '0' give you the int value 0, subtracting '0' from any char in the '0' to '9' range will give you the corresponding int value. The fact that '0'’s encoded value is 48 is not even relevant to this logic.

Mind the existence of the codepoints() method which returns a sequence of Unicode code point values in the range 0 to 1,114,111 (0x10FFFF). For characters in the BMP, the values are identical to the char values, but values outside the BMP are encoded as two UTF-16 units, so it’s easier to process them with the codepoints() stream instead.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.