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"

1

4 Answers 4

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}

Sign up to request clarification or add additional context in comments.

2 Comments

long x = Integer.MAX_VALUE+10; overflows too, since the addition is performed at type int. Use 10L or a cast to have the addition done at type long.
@DanielFischer:Good catch!Updated answer
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.

Comments

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;
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.