Adding and Subtracting Chars, Why Does This Work

Adding and subtracting chars, why does this work?

From the Docs

The char data type is a single 16-bit Unicode character.

A char is represented by its code point value:

  • min '\u0000' (or 0)
  • max: '\uffff' (or 65,535)

You can see all of the English alphabetic code points on an ASCII table.

Note that 0 == \u0000 and 65,535 == \uffff, as well as everything in between. They are corresponding values.

A char is actually just stored as a number (its code point value). We have syntax to represent characters like char c = 'A';, but it's equivalent to char c = 65; and 'A' == 65 is true.

So in your code, the chars are being represented by their decimal values to do arithmetic (whole numbers from 0 to 65,535).

For example, the char 'A' is represented by its code point 65 (decimal value in ASCII table):

System.out.print('A'); // prints A
System.out.print((int)('A')); // prints 65 because you casted it to an int

As a note, a short is a 16-bit signed integer, so even though a char is also 16-bits, the maximum integer value of a char (65,535) exceeds the maximum integer value of a short (32,767). Therefore, a cast to (short) from a char cannot always work. And the minimum integer value of a char is 0, whereas the minimum integer value of a short is -32,768.


For your code, let's say that the char was 'D'. Note that 'D' == 68 since its code point is 68.

return 10 + ch - 'A';

This returns 10 + 68 - 65, so it will return 13.

Now let's say the char was 'Q' == 81.

if (ch >= 'A' && ch <= 'F')

This is false since 'Q' > 'F' (81 > 70), so it would go into the else block and execute:

return ch - '0';

This returns 81 - 48 so it will return 33.

Your function returns an int type, but if it were to instead return a char or have the int casted to a char afterward, then the value 33 returned would represent the '!' character, since 33 is its code point value. Look up the character in ASCII table or Unicode table to verify that '!' == 33 (compare decimal values).

Java: What does subtracting a char by a char mean?

The goal is count the occurrences of each character.

c - 'a'

is a kind of clever way to get the position of the character in the alphabet. 'a' - 'a' would give you 0. 'b' - 'a' would give you 1. 'c' - 'a' would give you 2, and so on.

That value is used as an index into the array (which as you correctly stated is initialised with zeros) and the count is incremented.


It's worth noting that this will break if any character other than a-z is present in the string (including uppercase characters), and you'd see an IndexOutOfBoundsException

How does subtraction between two Chars work?

It subtracts the ASCII value of the 2 characters.

A char is actually just stored as a number (its code point value). We have syntax to represent characters like char c = 'A';, but it's equivalent to char c = 65; and 'A' == 65 is true.

So in your case, the value at letters[0] will keep incrementing for every 'a' in the string and so on.

Thus at each position, you get the frequency of each letter.

Java: Subtract '0' from char to get an int... why does this work?

That's a clever trick. char's are actually of the same type / length as shorts. Now when you have a char that represents a ASCII/unicode digit (like '1'), and you subtract the smallest possible ASCII/unicode digit from it (e.g. '0'), then you'll be left with the digit's corresponding value (hence, 1)

Because char is the same as short (although, an unsigned short), you can safely cast it to an int. And the casting is always done automatically if arithmetics are involved

Addition/Subtraction of char and specifiers in printf statement in C

As @tadman mentioned a char in C is just a numerical value.

So c1 boils down to 'A' (=65) + '8' (=56) - '4' (=52) = 69
and c2 boils down to 'A' (=65) + '8' (=56) - '5' (=53) = 68

the format specifiers in the printf statement just changes how these numerical values are printed.

Why does subtracting '0' in C result in the number that the char is representing?

Because the char are all represented by a number and '0' is the first of them all.

On the table below you see that:

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

'9' => 57.

As a result: ('9' - '0') = (57 − 48) = 9

Sample Image
Source: http://www.asciitable.com



Related Topics



Leave a reply



Submit