Characters are a kind of integer in Java; the integer is a number associated with the character on the Unicode chart. Thus, 'a'
is actually the integer 97; 'b'
is 98, and so on in sequence up through 'z'
. So s.charAt(i)
returns a character; assuming that it is a lower-case letter in the English alphabet, subtracting 'a'
from it gives the result 0 for 'a'
, 1 for 'b'
, 2 for 'c'
, and so on.
You can see the first 4096 characters of the Unicode chart at http://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF (and there will be references to other pages of the chart as well). You'll see 'a'
there as U+0061 (which is hex, = 97 decimal).