Java

How to Find Max Value of int in Java

In Java, “int” is a primitive data type that holds numeric values and requires 32 bits (4 bytes) to store data in memory. Furthermore, they are defined in binary form using 2’s complement; one of the 32 bits is a sign bit. As Java encourages signed numbers, the int range contains both positive and negative values. We need to find maximum and minimum numbers in a variety of situations.

This tutorial explains the method to find the maximum value of int in Java.

How to Find Max Value of int in Java?

For finding the maximum value of int in Java, use the “MAX_VALUE” constant of the Integer class. In Java, the int type has a maximum value of “2147483647”. It is difficult to memorize the exact value; that’s why Java stored this value as a static constant.

Now, let’s check out some examples related to the usage of the Integer.MAX_VALUE constant.

Example 1: Print Max Value of Static Integer Constant in Java
In this example, we will print the max value of the int by using the “System.out.println()” method:

 System.out.println("The max value of int is:" + Integer.MAX_VALUE);

The output indicates that the maximum value of int is “2147483647”:

Example 2: Printing Max Value of User-defined Variable in Java
Here, we will print the maximum value of a user-defined variable. To do so, we will create an integer type variable “x” with the “35” value:

int x = 35;

Then, we will create one more integer type variable, “y”, that stores the maximum value of int using “Integer.MAX_VALUE”. Here, we will pass the variable “x” as an object of the Integer class to access the MAX_VALUE constant:

int y = ((Integer)x).MAX_VALUE;

Finally, we will print the maximum value of the created integer:

System.out.println("The max value of int 'x' is:" + y);

The output represents the maximum value of int as a maximum value for integer x:

Example 3: Adding a Number in Max Value of int in Java
The maximum value indicates the range of the int primitive type which is 2147483647. If we try to add a number to the max value, it will overflow the memory and return the negative value.

In this example, we will add the “1” in MAX_VALUE and store it in the integer type variable “x”:

int x = Integer.MAX_VALUE + 1;

Then, print the value of “x” using the “System.out.println()” method:

System.out.println("x = " + x);

The output displays the negative value, which refers to the memory overflow:

We have covered the fundamentals of finding the maximum value of an int in Java.

Conclusion

You can find the max value of int with the help of the “MAX_VALUE” constant of the Integer class. The maximum value indicates the maximum range of int primitive type, which is “2147483647”. It is difficult to memorize the whole constant value; that’s why Java offers the Integer.MAX_VALUE static constant to find the max-value of int. In this tutorial, we explained the procedure for finding and using the max value of int in Java with examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.