Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Largest decimal literal of type int is 2147483648 or 2147483647?

Tags:

java

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?

like image 274
hagrawal Avatar asked Oct 10 '15 22:10

hagrawal


3 Answers

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.

like image 103
5gon12eder Avatar answered Nov 15 '22 10:11

5gon12eder


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.

like image 20
Reimeus Avatar answered Nov 15 '22 12:11

Reimeus


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.

like image 39
nanofarad Avatar answered Nov 15 '22 10:11

nanofarad