Java

Integer.MAX_VALUE in Java with Examples

The “Integer.MAX_VALUE” in Java is effective as it can be utilized to automatically allocate any variable to the maximum integer without remembering the exact number. In addition to that, this feature can be utilized by the developer in various mathematical computations to refrain from getting a garbage value or applying a check upon the maximum limit.

This article will elaborate on the usage and implementation of the “Integer.MAX_VALUE” in Java.

What is “Integer.MAX_VALUE” in Java?

The “Integer.MAX_VALUE” corresponds to the maximum positive integer value that can be represented in “32” bits (i.e., 2147483647). This implies that no number of type “Integer” can be greater than “2147483647” in Java.

Example 1: Applying the “Integer.MAX_VALUE” in Java to Return the Maximum Integer

In this example, the “Integer.MAX_VALUE” can be applied to return the maximum positive integer in “32” bits, as follows:

System.out.println("The maximum positive integer is: "+Integer.MAX_VALUE);

Output

In this output, it can be seen that the maximum positive integer is returned.

Example 2: Applying the “Integer.MAX_VALUE” in Java to Return the Valid Sum of the User Input Integers

In this particular example, the “Integer.MAX_VALUE” can be utilized to apply a check upon the sum of the user input integers and execute the corresponding “if/else” condition.

First of all, make sure to include the following library to enable user input:

import java.util.Scanner;

Now, add the below-provided code in the “main()” method:

Scanner obj = new Scanner(System.in);
System.out.println("Enter the first integer: ");
Integer a = obj.nextInt();
System.out.println("Enter the second integer: ");
Integer b = obj.nextInt();
Integer c = a + b;
System.out.println("The sum is: "+c);
if (c<Integer.MAX_VALUE & c>0) {
System.out.println("The integer is valid!");
}
else {
System.out.println("The integer is invalid!");
}

In the above code snippet:

  • Create a “Scanner” object using the “new” keyword and the “Scanner()” constructor, respectively.
  • The “System.in” parameter is used to take user input.
  • In the next step, input the integers from the user via the “nextInt()” method that needs to be added.
  • After that, return the sum of the user input integers.
  • Now, apply a check upon the computed sum such that it is less than the maximum positive integer and greater than “0”, respectively for being valid and display the corresponding message.
  • Note: The latter condition in the “if” statement is applied since upon exceeding the positive integer limit, the sum is returned in a negative manner, as follows:

In this outcome, since the computed value is greater than the maximum positive integer, therefore the negative sign “” is placed prior to the integer.

Output

In this output, it can be implied that both conditions are checked appropriately.

Conclusion

The “Integer.MAX_VALUE” in Java points to the maximum positive integer value that can be represented in “32” bits (i.e., 2147483647). This value can be utilized in various case scenarios to apply a check on the unlikely outcome. This blog discussed the usage and implementation of “Integer.MAX_VALUE” in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.