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:

2147483647

Minimum value of Integer:

-2147483648

Example 1

Following is an example to display the minimum and maximum value of int datatype:

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

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:

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

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.

Java Program to check for Integer Overflow
Java Program to get maximum of three integer values
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment