How to find Integer MAX and MIN Value in Java

To find Integers (or primitive int datatype) max and min values you can print use of the Integer wrapper class Integer.MAX_VALUE and Integer.MIN_VALUE constant values.

You can see that these are defined as static final values as hexadecimal values in java.lang.Integer class.

     * A constant holding the minimum value an int can have, -231. 
     public static final int   MIN_VALUE = 0x80000000;

     * A constant holding the maximum value an int can have, 231-1.
    public static final int   MAX_VALUE = 0x7fffffff;



public class Main {

    public static void main(String[] args) {

        int intMaxValue = Integer.MAX_VALUE;
        int intMinValue = Integer.MIN_VALUE;

        System.out.println("int/Integer Min Value in Java: " + intMinValue);
        System.out.println("int/Integer Max Value in Java: " + intMaxValue);



int/Integer Min Value in Java: -2147483648
int/Integer Max Value in Java: -2147483648

Caution with Integer Overflow and Underflow issues

    Be very careful when working with them, as you can end up with overflow and underflow issues which are hard to debug at times.

    Overflow Example:

    int intMaxValuePlusOne = Integer.MAX_VALUE + 1;
    System.out.println(Integer.MAX_VALUE +" + 1 = "+ intMaxValuePlusOne);

    2147483647 + 1 = -2147483648

    As you can see when I just add 1 to the integers max value, we get an unexpected answer. This is called overflow.

    Underflow Example:

    int intMinValuePlusOne = Integer.MIN_VALUE + 1;
    System.out.println(Integer.MAX_VALUE +" - 1 = "+ intMinValuePlusOne);

    -2147483648 - 1 = 2147483647

    As you can see when I just subtract 1 from the integer's min value, we get an unexpected answer. This is called underflow.

    To avoid make sure you know the range of the values you are dealing with, or make use of subtractExact() and addExact() static methods form java.math package that was added in Java 8 to deal with underflow and overflow issues.


    Integer intMinValueMinusOne = Math.subtractExact(Integer.MIN_VALUE, 1);
    System.out.println(Integer.MIN_VALUE +" - 1 = "+ intMinValueMinusOne);

    Exception in thread "main" java.lang.ArithmeticException: integer overflow
               at java.base/java.lang.Math.subtractExact(

    Exception in thread "main" java.lang.ArithmeticException: integer underflow
               at java.base/java.lang.Math.addtExact(

    That's better! You can simply add a try-catch block and handle it accordingly.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap