Is the largest decimal literal of type int - 2147483648 or 2147483647?

According to JLS §3.10.1

The largest decimal literal of type int is 2147483648.

Can this statement be considered true, because it Integer.MAX_VALUEis 2147483647?

Note that the emphasis in the above expression is on "int". If it is claimed to be discussed in the context of the “decimal literal,” then even 2147483649so on. Should also be true.

So, if something has a type int, then its greatest value should be 2147483647.

Am I mistaken or should this statement be updated?

+4
source share
4 answers

, Integer.MIN_VALUE is & minus; 2147483648. , -2147483648 " 2147483648". , 2147483648 int int Integer.MIN_VALUE .

: JLS , . . .

+3

JLS

2147483648

i.e

int value = -2147483648;

int value = 2147483648;

- .

+2

( , , ..), / . , 2147483647 , 2147999999 (while 2147999999L is, ). , , .

+1

. Reimeus .


, , JLS

int - 2147483648 (2 ^ 31)

    int j = 2147483648;

    Error:(20, 17) java: integer number too large: 2147483648

2 ^ 31 2147483648, 0x80000000, 32- -1.

, 2 ^ 31 int.

int can only represent values ​​from Integer.MIN_VALUE, which is -2 ^ 31, to Integer.MAX_VALUE, which is (2 ^ 31) -1. And, fortunately, the compiler does not accept integer literals outside this range.

+1
source

Source: https://habr.com/ru/post/1611127/


All Articles