Java: The Importance of Choosing intValue() or equals() When Comparing Integer Objects’ Values
This post shares an observation on Integer Comparisons in Java that I noticed during one coding session.
As is well-known, when comparing two integer variables of primitive type in Java, we can simply use the “==” operator.
However, there are instances where we may define integer values using variables of the Integer class data type (Integer class: a wrapper or boxed class for the primitive data type int).
When comparing variables of the Integer class data type using the “==” operator, it is generally not recommended. But let’s run two test cases and see the difference in output. This will help us understand why “==” operator is not recommended here.
Case 1: Both variables are having value 128. Result will be that two variables are not equal:
In this scenario, I assigned the same value of 128 to both variables. However, when using the “==” operator for comparison, objects are compared rather than their values. Consequently, the comparison may indicate that the two variables are not equal, despite having the same value assigned to them.
Case 2: If the value in both variables is 127, the same program can actually return true. Surprisingly, in Java, these two variables are considered equal in this case.
Here the Java compiler performs automatic boxing and caching for Integer objects. Therefore, both the variables will refer to the same cached Integer object.
For values within the range of -128 to 127, Java uses a cache for Integer objects to optimize memory usage. When comparing two Integer objects within this range, the “==” operator will return true if they have the same value, as they are referring to the same cached object.
However, for values outside this range, Java creates separate Integer objects for each value. When comparing two Integer objects outside the range of -128 to 127 using the “==” operator, it will return false, even if they have the same value. This is because they are distinct objects in memory.
To compare two Integer values in Java, it is recommended to use the equals()
method or the intValue()
method of the Integer class.
Using the equals()
method ensures a reliable comparison based on the numerical value of the Integers. Similarly, the intValue()
method extracts the primitive int
value from the Integer objects, allowing for direct comparison using the ==
operator. Both approaches are acceptable and can be used based on your preference or specific requirements.
public class IntegerComparison {
public static void main(String[] args) {
Integer num1 = 128;
Integer num2 = 128;
if (num1.equals(num2)) {
System.out.println("Variables are equal (Used equals method of Integer class).");
} else {
System.out.println("Variables are not equal");
}
// Alternatively, using the intValue() method
if (num1.intValue() == num2.intValue()) {
System.out.println("Variables are equal (Used intValue method of Integer class).");
} else {
System.out.println("Variables are not equal");
}
}
}
Thank you for taking the time to read the article. I aimed to keep it concise and clear.