Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I generate char beyond max value?

Tags:

java

char

Why does the following code not generate an error?

System.out.println((char) 2147483647);

According to oracle datatypes, the maximum size for a char is 65,535.

  • char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
like image 239
uranibaba Avatar asked Jun 12 '15 11:06

uranibaba


2 Answers

Why can I generate char beyond max value?

2147483647 is not a char but an int.

You're not assigning an invalid value to a char, you're casting a valid int to char and then Narrowing Primitive Conversion rules apply. See Java specs: §5.1.3.

In short you keep lowest 16 bits of original integer ("A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.").

Why does the following code not generate an error?

Because it's not an error, it's a well-defined behavior.

like image 177
Adriano Repetti Avatar answered Sep 28 '22 01:09

Adriano Repetti


You do a narrowing conversion from int to char, which is allowed see java spec: 5.1.3. Narrowing Primitive Conversion:

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

The resulting char isn't larger than Character.MAX_VALUE. The compiler converts (char) 2147483647 to 65535

like image 28
fabian Avatar answered Sep 28 '22 03:09

fabian