Create an int Overflow in Java
Description
The following code shows how to create an int Overflow.
Example
//from w w w . j av a 2 s. c o m
public class Main {
public static void main(String[] unused) {
do_shorts();
do_ints();
}
protected static void do_shorts() {
short i = Short.MAX_VALUE;
System.out.println("i=" + i++);
System.out.println("i=" + i++);
System.out.println("i=" + i++);
}
protected static void do_ints() {
int i = Integer.MAX_VALUE;
System.out.println("i=" + i++);
System.out.println("i=" + i++);
System.out.println("i=" + i++);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »