int整型溢出
int最大值和最小值为:
/** * A constant holding the minimum value an {@code int} can
* have, -2<sup>31</sup>.
*/
@Native public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
当最大值加上 1 时,结果反而变成表示范围中最小的值
当最小值减上 1 时,结果反而变成表示范围中最大的值
int x = java.lang.Integer.MAX_VALUE;System.out.println("x = " + x);
System.out.println("x + 1 = " + (x + 1));
System.out.println("x + 3 = " + ((long) x + 3));
System.out.println("--------------");
int y = Integer.MIN_VALUE;
System.out.println("y = " + y);
System.out.println("y - 1 = " + (y - 1));
System.out.println("y - 3 = " + ((long) y - 3));
输出如下:
x = 2147483647x + 1 = -2147483648
x + 3 = 2147483650
--------------
y = -2147483648
y - 1 = 2147483647
y - 3 = -2147483651
参考链接:
java-int数据的溢出 - 对林风眠 - 博客园
以上是 int整型溢出 的全部内容, 来源链接: utcz.com/z/511744.html