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;
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))
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
