Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples - GeeksforGeeks

22 Jan, 2020
Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold, but remembering such a large and precise number comes out to be a difficult job. Therefore, Java has constants to represent these numbers, so that these can be directly assigned to the variable without actually typing the whole number.
Integer.MAX_VALUEInteger.MAX_VALUE is a constant in the Integer class of java.lang package that specifies that stores the maximum possible value for any integer variable in Java. The actual value of this is2^31-1 = 2147483647
Example 1:// Java program to show// the value of Integer.MAX_VALUE  class GFG {      // Driver code    public static void main(String[] arg)    {          // Print the value of Integer.MAX_VALUE        System.out.println("Integer.MAX_VALUE = "                           + Integer.MAX_VALUE);    }}Output:Integer.MAX_VALUE = 2147483647
Any integer variable cannot store any value beyond this limit. Upon doing so, the memory will overflow and the value will get negative.Example 2: Trying to initialize a variable value Integer.MAX_VALUE + 1// Java program to show what happens when// a value greater than Integer.MAX_VALUE// is stored in an int variable  class GFG {      // Driver code    public static void main(String[] arg)    {          try {              System.out.println(                "Trying to initialize"                + " a N with value"                + " Integer.MAX_VALUE + 1");              // Try to store value Integer.MAX_VALUE + 1            int N = Integer.MAX_VALUE + 1;              // Print the value of N            System.out.println("N = " + N);        }        catch (Exception e) {            System.out.println(e);        }    }}Output:Trying to initialize a N with value Integer.MAX_VALUE + 1
N = -2147483648

2^31-1 = 2147483647

Example 1:
// Java program to show// the value of Integer.MAX_VALUE  class GFG {      // Driver code    public static void main(String[] arg)    {          // Print the value of Integer.MAX_VALUE        System.out.println("Integer.MAX_VALUE = "                           + Integer.MAX_VALUE);    }}
Integer.MAX_VALUE = 2147483647

Any integer variable cannot store any value beyond this limit. Upon doing so, the memory will overflow and the value will get negative.
Example 2: Trying to initialize a variable value Integer.MAX_VALUE + 1
// Java program to show what happens when// a value greater than Integer.MAX_VALUE// is stored in an int variable  class GFG {      // Driver code    public static void main(String[] arg)    {          try {              System.out.println(                "Trying to initialize"                + " a N with value"                + " Integer.MAX_VALUE + 1");              // Try to store value Integer.MAX_VALUE + 1            int N = Integer.MAX_VALUE + 1;              // Print the value of N            System.out.println("N = " + N);        }        catch (Exception e) {            System.out.println(e);        }    }}
Trying to initialize a N with value Integer.MAX_VALUE + 1
N = -2147483648

Integer.MIN_VALUEInteger.MIN_VALUE is a constant in the Integer class of java.lang package that specifies that stores the minimum possible value for any integer variable in Java. The actual value of this is-2^31 = -2147483648
Example 3:// Java program to show// the value of Integer.MIN_VALUE  class GFG {      // Driver code    public static void main(String[] arg)    {          // Print the value of Integer.MIN_VALUE        System.out.println("Integer.MIN_VALUE = "                           + Integer.MIN_VALUE);    }}Output:Integer.MIN_VALUE = -2147483648
Any integer variable cannot store any value below this limit. Upon doing so, the memory will overflow and the value will get positive.Example 2: Trying to initialize a variable value Integer.MIN_VALUE – 1// Java program to show what happens when// a value less than Integer.MIN_VALUE// is stored in an int variable  class GFG {      // Driver code    public static void main(String[] arg)    {          try {              System.out.println(                "Trying to initialize"                + " a N with value"                + " Integer.MIN_VALUE - 1");              // Try to store value Integer.MIN_VALUE - 1            int N = Integer.MIN_VALUE - 1;              // Print the value of N            System.out.println("N = " + N);        }        catch (Exception e) {            System.out.println(e);        }    }}Output:Trying to initialize a N with value Integer.MIN_VALUE - 1
N = 2147483647

-2^31 = -2147483648

Example 3:
// Java program to show// the value of Integer.MIN_VALUE  class GFG {      // Driver code    public static void main(String[] arg)    {          // Print the value of Integer.MIN_VALUE        System.out.println("Integer.MIN_VALUE = "                           + Integer.MIN_VALUE);    }}
Integer.MIN_VALUE = -2147483648

Any integer variable cannot store any value below this limit. Upon doing so, the memory will overflow and the value will get positive.
Example 2: Trying to initialize a variable value Integer.MIN_VALUE – 1
// Java program to show what happens when// a value less than Integer.MIN_VALUE// is stored in an int variable  class GFG {      // Driver code    public static void main(String[] arg)    {          try {              System.out.println(                "Trying to initialize"                + " a N with value"                + " Integer.MIN_VALUE - 1");              // Try to store value Integer.MIN_VALUE - 1            int N = Integer.MIN_VALUE - 1;              // Print the value of N            System.out.println("N = " + N);        }        catch (Exception e) {            System.out.println(e);        }    }}
Trying to initialize a N with value Integer.MIN_VALUE - 1
N = 2147483647

Java-Integer
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java