What does '0' do in Java?

I'm trying to find the product of all digits in a number, which I have stored as a string (due to int and long length limits).

So the number looks like this:

final String number = "1234567898765432123......etc" 

If I use this code, it works:

product *= number.charAt(i + j) - '0'; 

It doesn't if I remove the '0'.

I got this code from another online resource. Can someone explain what the '0' does?

1

6 Answers

Ascii characters are actually numbers. And 0 .. 9 digits are numbers starting from decimal 48 (0x30 hexadecimal).

'0' is 48 '1' is 49 ... '9' is 57 

So to get the value of any character digit, you can just remove the '0', ie 48.

'1' - '0' => 1 49 - 48 => 1 

If you don't remove '0' (48) the total sum will be over by 48 * numberOfDigits.

See an ascii table to locate digits in it.

Note that '0' is the character 0, not the string "0" containing the character '0'.

Let's check the ASCII chart enter image description here

You can see the Dec value of '0' -> '9'

The charAt() method just gets the character, is automatically converted to int value when calculating.

'0' -> 48 '1' -> 49 ... '9' -> 57 

after minus '0':

'0' - '0' -> 48 - 48 = 0 '1' - '0' -> 49 - 48 = 1 ... '9' - '0' -> 57 - 48 = 9 

After all, 'n' - '0' = n with n is a digit character

Removing 0 from number.charAt(i + j) will give you same character in form of digit (or integer).

When you are using character in any expression that character is replaced by its ASCII values.

ASCII value for '0'(character 0) is 48 ASCII value for '1'(character 0) is 49

Now, if number.charAt(i + j) is '1' then in your expression '1' is replaced by 49 and '0' is replaced by 48 which will give you 1(integer) as a result.

Characters and integers are practically the same thing because every character has a code (ASCII, but also Unicode in some contexts).

By subtracting '0' from whatever number is in the string, you get the actual value of that integer.

'0' > 48 48 - 48 = 0 (integer value) 

The same applies to all the other integers. You can do similar things with other characters, such as letter - 'A' to assign a number to every letter based on its position in the alphabet.

'0' is the char value of zero.

When you write a string, you're writing an array of 'char' datatypes which the compiler translates into ASCII values (which have a corresponding decimal number value).

When you call

number.charAt(i + j); 

You are actually generating an ASCII value.

The ASCII value of '0' is:

48

'1':

49

. . .

'9':

57

So when you call

number.charAt(i + j) - '0'; 

You're actually running the operation of an ASCII value minus 48 which is it's actual value as an integer.

e.g.

('9' - '0') = (57 - 48) = (9 - 0) = 9

Characters are stored as numbers. For example, '0' equals 48 in ASCII. So if you want to convert a character into an integer, you have to remove '0' (or remove 48 would work the same way):

product *= number.charAt(i + j) - 48; 

More information:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like