java 整数最小值和最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17392048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:51:31  来源:igfitidea点击:

Integer min and max values

javatypesinteger

提问by Ted Hopp

I am a newbie in programming. I was studying from a Java object programming book and performing the tutorials and examples on the book simultaneously on computer. In the book it says that the maximum and minimum value of integers are;

我是编程的新手。我正在学习一本 Java 对象编程书,并在计算机上同时执行书中的教程和示例。在书中它说整数的最大值和最小值是;

Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648

So OK. there is no problem here but; it says that if we add 1 to the maximum value and subtract 1 from minimum;

那么好吧。这里没有问题,但是;它表示如果我们在最大值上加 1 并从最小值中减去 1;

class test {
public static void main(String[] args) {

int min = Integer.MIN_VALUE -1;
int max = Integer.MAX_VALUE +1;

int a = min - 1;
int b = max + 1;
System.out.println("min - 1 =" + a);
System.out.println("max - 1 =" + b);
}
}

thus we find;

因此我们发现;

min - 1 = 2147483646
max + 1 = -2147483647

and it says that this result is because that the binary process in memory which is limited with 32 bit. The thing that I couldn't understand. In the piece of code isn't it adding and subtracting 2 respectively from maximum and minimum values?;

它说这个结果是因为内存中的二进制进程限制为 32 位。我无法理解的事情。在这段代码中,不是分别从最大值和最小值中加减2吗?

    int min = Integer.MIN_VALUE -1; // subtracted 1 here
int max = Integer.MAX_VALUE +1; // added 1 here

int a = min - 1;  // subtracted 1 here
int b = max + 1; // added 1 here

回答by Ted Hopp

Note that ais Integer.MAX_VALUE - 1and bis Integer.MIN_VALUE + 1. So yes, it is indeed subtracting and adding 1 twice in each case. The book is not wrong, but it's a stupid way of teaching about wrap-around overflow. Just printing Integer.MIN_VALUE - 1and Integer.MAX_VALUE + 1would have made the point.

请注意aisInteger.MAX_VALUE - 1bis Integer.MIN_VALUE + 1。所以是的,它确实在每种情况下减去和加 1 两次。这本书没有错,但它是一种关于环绕溢出的愚蠢教学方式。只是打印Integer.MIN_VALUE - 1Integer.MAX_VALUE + 1就可以说明这一点。

int min = Integer.MIN_VALUE -1; // min is set to Integer.MAX_VALUE by underflow
int max = Integer.MAX_VALUE +1; // max is set to Integer.MIN_VALUE by overflow

From the Java Language Specification, §15.18.2:

来自Java 语言规范第 15.18.2 节

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format.

如果整数加法溢出,则结果是数学和的低位,以某种足够大的二进制补码格式表示。

The JLS is the ultimate authority when it comes to questions like this, but I don't recommend reading it as a way to learn Java. You'd be better off going through the Java Language Tutorial. It's fairly comprehensive and the content is very high quality.

JLS 是此类问题的终极权威,但我不建议将其作为学习 Java 的一种方式。您最好阅读Java Language Tutorial。它相当全面,内容质量非常高。