What is Integer.MAX_VALUE in Java?
public class MaximumInteger {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
}
}
Output
2147483647
What are Integers in Java?
Integers are numbers that have no fractional part. In Java, Integers are represented in 32 bits space. In addition, they are represented in 2’s complement binary form, which means that one bit out of these 32 is a sign bit. Thus, there are 231-1 possible values. Hence, there is no integer greater than the number 231-1 in Java.Why is Integer.MAX_VALUE in Java Required?
It is used to automatically assign any variable the maximum integer possible without requiring to remember the exact number. There are many times when we need a maximum or minimum number. It can be for comparative reasons or any other. It can be difficult to remember the exact constant. This job is made easy by Integer.MAX_VALUE in Java.Example
public class MaximumInteger {
public static void main(String[] args) {
int maxNumber = Integer.MAX_VALUE;
System.out.println("maxNumber: " + maxNumber);
int number1 = Integer.MAX_VALUE - 1;
System.out.println("number1: " + number1);
if (number1 < maxNumber) {
System.out.println("number1 < maxNumber");
}
}
}
Output
maxNumber: 2147483647
number1: 2147483646
number1 < maxNumber
Explanation
In the code snippet above, we take a variable maxNumber and assign it maximum integer value using Integer.MAX_VALUE. Then we take another variable number1 and assign it a value one smaller than the maximum. We compare the two and print the results.Conclusion
By the end of this post, we hope you have got yourself familiarized with the Integer.MAX_VALUE in Java in detail. You have learned how to use Integer.MAX_VALUE in Java with examples. You can try assigning other values to different variables and see how this concept works to understand it in more depth. Keep practicing for a deeper command of the concept. Till then, keep growing and keep shining!
GO TO FULL VERSION