Integer Overflow

Lab Java Intermediate 20 points

Print Integer.MAX_VALUE, then Integer.MAX_VALUE + 1 (as an int), then the same sum computed as a long.

Input

None.

Output

Three lines.

Example

Output

2147483647
-2147483648
2147483648
Sample output available
Editor
Java

Hints

An int has 32 bits; going past the maximum wraps around to the minimum.

Cast to long before adding.

Solution explanation

Overflow is silent in Java. Use long (64 bits) or Math.addExact when values can get large.

Show solution
public class Main {
    public static void main(String[] args) {
        int max = Integer.MAX_VALUE;
        System.out.println(max);
        System.out.println(max + 1);
        System.out.println((long) max + 1);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD