Q:

How to check if number is above Integer.MAX_VALUE in Java, when adding to it some other number?

I have a problem when I add some number to the maximum of Integer, I am no longer above the maximum because the number changes to Integer.MIN_VALUE + added valued -1, how should I than check it ? 
`
        int max = Integer.MAX_VALUE;
        int x = max +100;
        if (x > max){
            System.out.println(x + "x is not bigger, so it wont be printed.");

        }

    }
}`

A:

There are a couple of methods depending on what you want to do.

If you are looking to add a number to an int and want to know if it overflows, then use Math.addExact. It throws an ArithmeticException which you can catch if you want to take action.
If you want to safely add a value and then compare to Integer.MAX_VALUE then use a long instead. By definition no int is ever larger than the maximum value.

So I'd suggest
try {
    int x = Math.addExact(Integer.MAX_VALUE, 100);
} catch (ArithmeticException ex) {
    System.out.println("x has overflowed");
}

