3

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 >= 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...

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 1000000100000 and it will respond with the answer as well but then someone might write something that exceeds the max/min values like, 100000001000000*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"

Positive Navid
  • 2,481
  • 2
  • 27
  • 41
Deragon
  • 305
  • 1
  • 4
  • 13
  • This is a duplicate of [How does Java handle integer underflows and overflows and how would you check for it?](http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo) – AffluentOwl Mar 14 '14 at 04:19

4 Answers4

7

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}

Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
Cratylus
  • 52,998
  • 69
  • 209
  • 339
6

If you need to represent integers which are larger or smaller than the limits, then use long instead of int variables. 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. By the time the value is stored in an int, any overflow or underflow has already occurred.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
2

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).

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.

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;
}
AffluentOwl
  • 3,337
  • 5
  • 22
  • 32
0

Integer.MAX_VALUE and MIN_VALUE are the largest/smallest values representable with an int. That's the point: you can't have an int with lesser or greater values.

If you want to hold and test against lesser or greater values, you'll need to use a long.

When you try to add to e.g. 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.

pb2q
  • 58,613
  • 19
  • 146
  • 147