What does ‘0’ do in Java?

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'.

Leave a Comment