Home > Java > javaTutorial > Java Integer Comparison: When Should I Use `==` vs. `equals()`?

Java Integer Comparison: When Should I Use `==` vs. `equals()`?

Barbara Streisand
Release: 2024-12-26 18:03:11
Original
456 people have browsed it

Java Integer Comparison: When Should I Use `==` vs. `equals()`?

Integer Comparison Conundrum in Java: == vs. equals()

In Java 1.5 and beyond, Integers can be treated as primitive int values in many scenarios. However, a recent code issue has raised questions about the validity of using the == operator for Integer comparisons.

Problem Statement

Consider the following code snippet:

Integer cdiCt = ...;
Integer cdsCt = ...;
...
if (cdiCt != null && cdsCt != null && cdiCt != cdsCt)
    mismatch = true;
Copy after login

This code appears to set mismatch to true when the values of cdiCt and cdsCt are equal. The issue becomes apparent when examining the boolean expression: it evaluates to false when the values are indeed equal, but somehow mismatch is still set to true when execution continues.

Resolution

The solution to this problem is to use the equals() method instead:

if (cdiCt != null && cdsCt != null && !cdiCt.equals(cdsCt))
Copy after login

Explanation

The == operator compares the memory references of two objects, while the equals() method compares their values. For primitive types like int, the == operator works as expected. However, for objects like Integer, the == operator only checks if the references point to the same cached value.

The JVM caches Integer values between -128 and 127 for performance reasons. Therefore, when comparing two Integers using ==, the operator will return true only if both values fall within this range. In the given scenario, the values are likely just outside this range, leading to the unexpected behavior.

Conclusion

While using == to compare primitives is acceptable, it is recommended to use the equals() method for comparing wrapped objects like Integer. This ensures consistent behavior regardless of the values involved.

Additional Resources

  • [Immutable Objects / Wrapper Class Caching](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html#Immutable_Objects_.2F_Wrapper_Class_Caching)
  • [Why can't the compiler/JVM just make autoboxing “just work”?](https://stackoverflow.com/questions/12595136/why-cant-the-compiler-jvm-just-make-autoboxing-just-work)

The above is the detailed content of Java Integer Comparison: When Should I Use `==` vs. `equals()`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template