Doubt about int variable range

I doubt the range of int values

int x=2147483647; /*NO Error--this number is the maximum range of int value no error*/ int y=2147483648; /*Error--one more than the maximum range of int*/ int z=2147483647+1; /*No Error even though it is one more than the maximum range value*/ 

why?

+4
source share
7 answers

Here is an explanation from the point of view of the Java language specification.

The section on integer literals ( JLS 3.10.1 ) says the following:

The largest decimal literal of type int is 2147483648 (2 31 ). All decimal literals from 0 to 2147483647 can appear wherever int literal can be displayed, but the literal 2147483648 can only be displayed as an operand of the unary negation operator - .

So...

  • The first statement is the assignment of a left integer literal value. Compilation error.

  • The second statement is a compilation error, because 2147483648 not preceded by a unary negation operator.

  • The third statement does not contain an integer literal that is out of range, therefore, from this point of view, this is not a compilation error.

Instead, the third statement is a binary addition, as described in JLS 15.18.2 . This expresses the following about the integer case:

If the integer addition overflows, then the result is the least significant bits of the mathematical sum, presented in some fairly large format with two additions. If overflow occurs, the sign of the result does not coincide with the sign of the mathematical sum of the two values ​​of the operand.

Thus, 2147483647 + 1 overflows and wraps up to -2147483648 .


@Peter Lawrey suggests (frivolously?) That the third statement can be "rewritten by the compiler" as +2147483648 , which leads to a compilation error.

This is not true.

There is nothing in JLS that suggests that a constant expression may have a different meaning for a mutable expression. In contrast, in cases like 1/0, JLS flips things around and says that the expression is NOT a BECAUSE constant expression because it aborts abnormally. (It is in JLS 15.28 )

JLS tries very hard to avoid cases where some kind of Java construct means different things, depending on the compiler. For example, it is very important to follow the rules of a “specific purpose” in order to avoid the case when only an intelligent compiler can infer that a variable is always initialized before it is used. This is a good idea in terms of code portability.

The only significant area where there is “room for maneuver” for compiler developers to perform specific platform tasks is in the concurrency and Java memory models. And there is a good pragmatic reason for this - to allow multi-threaded Java applications to quickly work on multi-core / multi-processor equipment.

+8
source

int ranges from Integer.MIN_VALUE (-2147483648) to Integer.MAX_VALUE (2147483647) .

However, only int literals are checked for range.

Java does not verify that any given constant expression matches within a range.

Computations are “allowed” to traverse these boundaries, but this will lead to overflow (that is, only the low-order bits of the resulting value will be saved). Therefore, the calculation of 2147483647 + 1 clearly defined in the framework of int calculations, and it is -2147483648.

+2
source

Because the third is called a whole overflow. You do the calculations and overflow. The rest are just constants.

+1
source

Because

 int z=2147483647+1; 

will be crowded which is not equal 2147483648

+1
source

The first two cases seem obvious. The third case will overwhelm silently. Therefore, in such cases, you should always handle this call code.

+1
source

the third expression is an int-based append, so it will cast the result to a value within the int interval.

0
source

The range for int is from Integer.MIN_VALUE to Integer.MAX_VALUE . Java is slightly crowded, so the result of the calculation is not detected by the compiler. (But can be detected by your IDE)

One of the most amazing overflow operations is -Integer.MIN_VALUE

0
source

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


All Articles