简体   繁体   English

java Integer.MAX_VALUE, MIN_VALUE 溢出

[英]java Integer.MAX_VALUE, MIN_VALUE overflow

Ok maybe I am just tired because of this but how can I accomplish this?好吧,也许我只是因为这个而累了,但我怎么能做到这一点?

int x = Integer.MAX_VALUE+10;

// or perhaps // 也许

int x = Integer.MIN_VALUE-20;

I just want the if statement to catch if x is "within range" kinda like this:我只想让 if 语句捕获 x 是否“在范围内”有点像这样:

if(x >= Integer.MAX_VALUE || x <= Integer.MIN_VALUE){ //throw exception}

But the problem is that if the value is as mentioned above, like MAX_VALUE + 10, the value ends up being neither higher than the MAX VALUE nor lower than the MIN_VALUE and the if-conditions aren't met...但问题是,如果该值如上所述,如 MAX_VALUE + 10,则该值最终既不高于 MAX VALUE 也不低于 MIN_VALUE 并且不满足 if 条件......

Edit: To clarify what I meant: I don't want to actually store any values bigger than the max/min value.编辑:澄清我的意思:我不想实际存储任何大于最大/最小值的值。 Imagine this:想象一下:

A field, you write 10+10 and it says like "Ok, that's 20" Next up, someone maybe will write 1000000 100000 and it will respond with the answer as well but then someone might write something that exceeds the max/min values like, 10000000 1000000*1000000 or someyhing like that and then the program should be all like "Hold up, that's way too high! here's a "0" for you instead"一个字段,你写 10+10,它会说“好吧,那是 20”接下来,有人可能会写 1000000 100000,它也会回复答案,但是有人可能会写一些超过最大值/最小值的东西,比如, 10000000 1000000*1000000 或类似的东西,然后程序应该都像“等等,这太高了!这里是一个“0”给你”

This can be solved in 2 ways:这可以通过两种方式解决:

First way:第一种方式:

if(x + 10 <= x){ //Has wrapped arround so throw exception}

Second way (better):第二种方式(更好):

long x = Integer.MAX_VALUE+10L;
Now the conditional works properly现在条件可以正常工作

if(x >= Integer.MAX_VALUE){ //throw exception}

If you need to represent integers which are larger or smaller than the limits, then use long instead of int variables.如果您需要表示大于或小于限制的整数,请使用long而不是int变量。 If, on the other hand, you're truly worrying about int variables that contain a value larger than Integer.MAX_VALUE , then stop worrying -- such a thing can't exist.另一方面,如果您真的担心int变量包含的值大于Integer.MAX_VALUE ,那么请不要担心——这样的事情不可能存在。 By the time the value is stored in an int , any overflow or underflow has already occurred.当值存储在int中时,任何溢出或下溢都已经发生。

The problem with all of the previously provided solutions is that they all recommend upcasting to a primitive which is of a larger size to perform your calculation (such as using long instead of int ).之前提供的所有解决方案的问题在于,它们都建议向上转换为更大尺寸的原语来执行您的计算(例如使用long而不是int )。

This approach is a little heavy handed and fails when you're already using the largest primitive.这种方法有点笨拙,当您已经在使用最大的原语时会失败。 Instead you can check to see if an operation, such as addition or subtraction, will overflow prior to performing the operation.相反,您可以在执行操作之前检查操作(例如加法或减法)是否会溢出。

Here's a function from The CERT Oracle Secure Coding Standard for Java , which also provides equivalent functions for the other mathematical operators, such as subtraction.这是来自The CERT Oracle Secure Coding Standard for Java的一个函数,它还为其他数学运算符提供等效函数,例如减法。

static final int safeAdd(int left, int right) throws ArithmeticException {
  if (right > 0 ? left > Integer.MAX_VALUE - right
                : left < Integer.MIN_VALUE - right) {
    throw new ArithmeticException("Integer overflow");
  }
  return left + right;
}

Integer.MAX_VALUE and MIN_VALUE are the largest/smallest values representable with an int . Integer.MAX_VALUEMIN_VALUE是可以用int表示的最大/最小值。 That's the point: you can't have an int with lesser or greater values.这就是重点:你不能有一个值更小或更大的int

If you want to hold and test against lesser or greater values, you'll need to use a long .如果你想保持和测试更小或更大的值,你需要使用long

When you try to add to eg Integer.MAX_VALUE and hold the result in an int , the variable will overflow , and the result will remain a value somewhere between MAX_VALUE and MIN_VALUE .当您尝试添加到例如Integer.MAX_VALUE并将结果保存在int中时,变量将溢出,结果将保持在MAX_VALUEMIN_VALUE之间的某个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM