As per JLS §3.10.1
The largest decimal literal of type int is 2147483648.
Can this statement be considered as true because Integer.MAX_VALUE
is 2147483647
?
Please note that emphasis in above statement is on "int". If it is argued that it is being talked in context of "decimal literal" then even 2147483649
and so on, should be also true.
So, if something is of type int
then its largest value has to be 2147483647
.
Am I getting it wrong or that statement should be updated?
Note that there are no negative integral literals and Integer.MIN_VALUE
is −2147483648. So -2147483648
is parsed as “apply unary minus to 2147483648”. It would be very bad if 2147483648 would not be a valid decimal int
literal or you couldn't use an int
literal of value Integer.MIN_VALUE
directly in your program.
Side note: The JLS defines what is correct. So it is correct by definition. It can be bad, though.
From the same JLS section
The decimal literal 2147483648 may appear only as the operand of the unary minus operator
i.e
int value = -2147483648;
exists
but
int value = 2147483648;
is a compile time error.
Every literal is of a specific type of literal (boolean literal, integer literal, floating point literal, etc), although it may be assigned to a field/variable of different type. For example, 2147483647
is a valid integer literal, while 2147999999
is not (while 2147999999L
is, although it is a long literal). While the writing is unclear there appears to be no contradiction of any sort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With