Non-numeric primitive casting
suggest changeThe boolean type cannot be cast to/from any other primitive type.
A char can be cast to or from any numeric type by using the code-unit mappings specified by Unicode. A char is represented in memory as an unsigned 16-bit integer value (2 bytes), so casting to byte (1 byte) will drop 8 of those bits (this is safe for ASCII characters). The utility methods of the Character class use int (4 bytes) for code points. A char holds one UTF-16 code unit, and a supplementary code point does not fit in a char or a short.
int badInt = (int) true; // Compiler error: incompatible types
char char1 = (char) 65; // A
byte byte1 = (byte) 'A'; // 65
short short1 = (short) 'A'; // 65
int int1 = (int) 'A'; // 65
char char2 = (char) 8253; // ‽
byte byte2 = (byte) '‽'; // 61 (truncated code-point into the ASCII range)
short short2 = (short) '‽'; // 8253
int int2 = (int) '‽'; // 8253
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents