Search code examples
javajls

Why does JLS state that the largest int literal is 2147483648?


JLS 3.10.1. Integer Literals http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1 states

The largest decimal literal of type int is 2147483648.

At the same time this line

int x = 2147483648;

produces a compile error

The literal 2147483648 of type int is out of range

Is JLS wrong?


Solution

  • The largest decimal literal of type int is 2147483648 (231).

    All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear. 
    

    It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).