Open In App

Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples

Last Updated : 22 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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_VALUE
    Integer.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 is

    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);
        }
    }

    
    

    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
    
  • Integer.MIN_VALUE
    Integer.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
    


Similar Reads

Integer.valueOf() vs Integer.parseInt() with Examples
Integer.parseInt():While operating upon strings, there are times when we need to convert a number represented as a string into an integer type. The method generally used to convert String to Integer in Java is parseInt(). This method belongs to Integer class in java.lang package. It takes a valid string as a parameter and parses it into primitive d
4 min read
Java Program to Convert Integer List to Integer Array
There are many ways to convert integer List to ArrayList where in this article we will be discussing out 2 approaches as below: Using concept od streams in Java8Using Apache Commons LangUsing Guava Library Method 1: Using concept od streams in Java8 So, first, we will get to know about how to convert integer list to array list using java 8. In java
3 min read
Program to Convert Set of Integer to Array of Integer in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. Java 8 Stream API can be used to convert Set to Se
2 min read
Converting Integer-String HashMap to String-Array-Integer HashMap in Java
Efficient Conversion of a HashMap<Integer, String> into another HashMap<String, Integer[]> involves organizing integer keys based on their corresponding string values. Here's a concise and efficient Java method to accomplish this transformation, focusing on clear and straightforward implementation. Step-by-Step Implementation of Convers
3 min read
Difference between an Integer and int in Java with Examples
In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.Integer is a class and thus it
4 min read
Java lang Integer.toHexString() Method with Examples
The Java.lang.Integer.toHexString() is a built-in function in Java which returns a string representation of the integer argument as an unsigned integer in base 16. The function accepts a single parameter as an argument in Integer data-type. Syntax : public static String toHexString(int num) Parameter : The function accepts a single mandatory parame
3 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
How to Read and Print an Integer value in Java
The given task is to take an integer as input from the user and print that integer in Java language. In the below program, the syntax and procedures to take the integer as input from the user are shown in Java language. Steps for InputThe user enters an integer value when asked.This value is taken from the user with the help of nextInt() method of
2 min read
Java.lang.Integer class and its methods
java.lang.Integer wraps integer data type to an object containing a single field having datatype is int. Constructors : Integer (int arg) : Constructs integer object representing an int value.Integer (String arg) : Constructs string object representing a string value.Integer class methods: toBinaryString() : java.lang.Integer.toBinaryString() metho
6 min read
Java 8 | Consumer Interface in Java with Examples
The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don't return any value.Hence this functional interface which takes in one generic namely:-
4 min read
Article Tags :
Practice Tags :