char

Figure 101. char Slide presentation
Code Result
char c;

c = '↔';       // Left-right arrow character

c = '\u2194';  // Same, in Unicode hexadecimal representation
               // 8596 = 16 * (16 * (2 * 16 + 1) + 9) + 4
               

c = 8596;      // Same, int decimal literal to char narrowing

IO.println(c);
IO.println((int) c);

8596

exercise No. 28

Show neighboring characters

Q:

Print the next 6 Unicode characters following '↔' similar to Figure 101, “char ” by using Unicode hexadecimal encoding.

Repeat the same exercise using regular decimal encoding. Hint: You have to turn int values into char ones. In Figure 101, “char ” it was done the other way round.

A:

Code Result Result
IO.println('\u2195');
IO.println('\u2196');
IO.println('\u2197');
IO.println('\u2198');
IO.println('\u2199');
IO.println('\u219A');
↕
↖
↗
↘
↙
↚
IO.println((char) 8597);
IO.println((char) 8598);
IO.println((char) 8599);
IO.println((char) 8600);
IO.println((char) 8601);
IO.println((char) 8602);

The ratio behind the above code: E.g. the '\u2195' literal is of type char and will thus be printed as such. Conversely the 8597 literal is of type int and would thus be printed as 8597. Therefore we need the so called cast operation (char) ... explicitly forcing the int into char.