Grails Cookbook - A collection of tutorials and examples

Maximum Value of Integer in Java

We've discussed in the previous post that an integer is a data type in Java, and we've determined that it's one of the main tools to use in calculations. Although with this information, it's necessary to remember that integers do not have a random large value, the Java Integer Maximum Value is 2,147,483,647. Check out below for more details.

Integer Maximum Value - What's it all about?

Like we've discussed in Java Long Maximum Value, integers also is represented with 32 bits. You would realize by now that Integer value can hold and is also restricted by this number of binary digits, and remember that we've tackled about how it can hold both positive and negative values. In the negative number spectrum, it can hold 31 bits combinations while from the range of 0 and positive values, another 31 bits can also be represented. Since 0 is in the spectrum of 0 and positive values, this spectrum can hold 2 raised to the power of 31 minus 1 is the maximum value integer. And just like what we've discussed in Java Long Max Value, it's necessary to account for the number 0, therefore, we need to diminish it by one. Let's not forget what the negative value can hold, it's minus 2 raised to the power of 31.

Let's now find out the expected result when we calculate 2 raised to the power of 31. I'ts 2,147,483,648, which is an even number. When we deduct one from the said value, we now have the maximum value for intergers - 2,147,483,647.

Check out the code snippet from the Integer class that defines the constant maximum value of Integer in Java (based from Java 1.5 source as of the time of writing):
public final class Integer extends Number implements Comparable<Integer> {
...
    /**
     * A constant holding the maximum value an {@code int} can
     * have, 2<sup>31</sup>-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff;
...
}
Also when we have this constant, it's needless to say that the value is 2,147,483,647. We can just utilize the Integer.MAX_VALUE in order to get the maximum Integer value. Check out the written example code for it's usage:
/**
 * This is a simple application that looks up the value of Integer.MAX_VALUE.
 **/
public class TestInteger
	public static void main(String[] args) {
		System.out.println("The maximum Integer value is: "+Integer.MAX_VALUE);
	}
}
The expected result of this code is this:
The maximum Integer value is: 2147483647
Let's now try what happens if we add 1 value to this number.
/**
 * This is a simple application that tries to add the value 1 to the maximum integer value:
 */
public class TestInteger {
	public static void main(String[] args) {
		int maxIntValue = Integer.MAX_VALUE;
		maxIntValue = maxIntValue + 1;
		System.out.println("The maximum Integer value plus one is: " + maxIntValue);
	}
}
We got the lowest negative number when we add 1 value to the maximum Integer value. Check out the output of the code below:
The maximum Integer value plus one is: -2147483648