18 Aug Java Program for Minimum and Maximum values of datatype int
Data Types in Java include the byte, short, boolean, int, long, float, double and char type. Every Java Datatype has a range, i.e. represented by a minimum and maximum value.
Maximum value of Integer:
1 2 3 |
2147483647 |
Minimum value of Integer:
1 2 3 |
-2147483648 |
Example 1
Following is an example to display the minimum and maximum value of int datatype:
1 2 3 4 5 6 7 8 9 10 |
public class Example { public static void main(String[] args) { System.out.println("Integer type MAXIMUM Value = " + Integer.MAX_VALUE); System.out.println("Integer type MINIMUM Value = " + Integer.MIN_VALUE); } } |
Output
1 2 3 4 |
Integer type MAXIMUM Value = 2147483647 Integer type MINIMUM Value = -2147483648 |
Example 2
Following is an example to check for Integer Overflow with the maximum and minimum values of datatype int:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Example { public static void main(String[] args) { int a = Integer.MAX_VALUE; System.out.println("Maximum value of Integer = "+a); // incrementing Maximum value of Integer int i = a + 1; System.out.println("Incrementing Maximum value of Integer = "+i); long k = (long)i; if (k > a) { throw new ArithmeticException("Integer Overflow!"); } } } |
Output
1 2 3 4 |
Maximum value of Integer = 2147483647 Incrementing Maximum value of Integer = -2147483648 |
On incrementing MAX_VALUE, it results in MIN_VALUE for Integer type in Java as shown above. Java handles overflow accordingly for Integer type.
No Comments