无法理解 Java 溢出和编译错误逻辑
Having trouble understanding Java Overflow and Compile error logic
我是Java新手,正在学习int overflow!
在玩一些整数时,我发现非常奇怪的结果
int x = 2147483647 + 1;
x == > - 2147483648
int x = 2147483647 + 2;
x ==> -2147483647
int x = 2147483647 + 2147483647;
x ==> -2
int x = 2147483647 + 2147483648;
**compile error**
我认为整数溢出不会导致任何编译错误。此外,我很难理解溢出的输出是如何计算的(例如,为什么 int x = 2147483647 + 1 // x ==> -2147483648)
任何人都可以解释这些结果的逻辑吗?
谢谢!!
作为前 3 个示例,您可以在计算值时得到 IntegerOverflow
这里关于 2147483648
的问题有点相同但有所不同,因为 Integer.MAX_VALUE
是 2147483647
你不能在 int
中存储更多,这就是为什么你得到一个编译错误 2147483648
太大而无法放入。即使在运行前,您已经无法将值放入 int
,这与运行时发生的溢出不同
The largest decimal literal of type int is 2147483648 (2^31).
All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear. The decimal literal 2147483648 may appear only as the operand of the unary minus operator - (§15.15.4).
It is a compile-time error if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator; or if a decimal literal of type int is larger than 2147483648 (2^31).
您不能将 2147483648 用作 int
文字,因为 int
文字是 int
表达式,因此必须具有 int
值;但是 2147483648 太大,不能用作 int
值。
Also, it is hard for me to understand how outputs for the overflow are
calculated (ex. why int x = 2147483647 + 1 // x ==> -2147483648)
无论何时发生上溢或下溢,该值都从另一端开始,例如
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MAX_VALUE + 1);// Will be equal to Integer.MIN_VALUE
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MIN_VALUE - 1);// Will be equal to Integer.MAX_VALUE
}
}
输出:
2147483647
-2147483648
-2147483648
2147483647
我是Java新手,正在学习int overflow! 在玩一些整数时,我发现非常奇怪的结果
int x = 2147483647 + 1;
x == > - 2147483648
int x = 2147483647 + 2;
x ==> -2147483647
int x = 2147483647 + 2147483647;
x ==> -2
int x = 2147483647 + 2147483648;
**compile error**
我认为整数溢出不会导致任何编译错误。此外,我很难理解溢出的输出是如何计算的(例如,为什么 int x = 2147483647 + 1 // x ==> -2147483648) 任何人都可以解释这些结果的逻辑吗?
谢谢!!
作为前 3 个示例,您可以在计算值时得到 IntegerOverflow
这里关于 2147483648
的问题有点相同但有所不同,因为 Integer.MAX_VALUE
是 2147483647
你不能在 int
中存储更多,这就是为什么你得到一个编译错误 2147483648
太大而无法放入。即使在运行前,您已经无法将值放入 int
,这与运行时发生的溢出不同
The largest decimal literal of type int is 2147483648 (2^31).
All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear. The decimal literal 2147483648 may appear only as the operand of the unary minus operator - (§15.15.4).
It is a compile-time error if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator; or if a decimal literal of type int is larger than 2147483648 (2^31).
您不能将 2147483648 用作 int
文字,因为 int
文字是 int
表达式,因此必须具有 int
值;但是 2147483648 太大,不能用作 int
值。
Also, it is hard for me to understand how outputs for the overflow are calculated (ex. why int x = 2147483647 + 1 // x ==> -2147483648)
无论何时发生上溢或下溢,该值都从另一端开始,例如
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MAX_VALUE + 1);// Will be equal to Integer.MIN_VALUE
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MIN_VALUE - 1);// Will be equal to Integer.MAX_VALUE
}
}
输出:
2147483647
-2147483648
-2147483648
2147483647