Java Puzzler, double chars
A teaser which uses char and doubleThe following compiles and produces an output. Can you explain why?
char ch = 'A'; ch *= 1.5; System.out.println(ch); // prints 'a'Here is a hint for a simpler example.
byte b = 100; b /= 2.5; System.out.println(b); // prints 40
'A' is 65.
'a' is 97
65 * 1.5 = 97.5
And since char is an integer...
char isn't an integer - it's unsigned integer
Or being pedantic, char is an unsigned short. ;)ReplyDelete
That's another java puzzler.ReplyDelete
JLS the 3rd edition:
The Unicode standard was originally designed as a fixed-width 16-bit character encoding. It has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, using the hexadecimal U+n notation.
So, it is not a 32-bit integer, in the same time it definitely more than short.
@Vladimir The String and Character classes now support "code points" which assume a String is UTF-16 encoded for characters. http://en.wikipedia.org/wiki/UTF-16ReplyDelete
In the case of Character there are pairs of methods.
public static boolean isLowerCase(char ch)
public static boolean isLowerCase(int codePoint)
Character.MAX_VALUE is still 65535, and char is still 16-bit, but code points use int values though as you point out, not the full range of 32-bit values. ;)