Why Does `==` Sometimes Fail to Compare `Integer` Objects Correctly in Java?
The Mystery of Integer Equality in Java
As Java introduced autoboxing in version 1.5, the use of Integer objects became interchangeable with primitive ints. However, it has been observed that using == for comparison of Integer objects may lead to unexpected results.
In a perplexing situation, code utilizing == for comparing Integer variables was found to incorrectly set a boolean flag as true when the values were in fact equal. However, switching the comparison to !cdiCt.equals(cdsCt) resolved the issue.
This unusual behavior arises from the JVM's caching mechanism for Integer values. For values within the range of -128 to 127, the JVM reuses cached instances, ensuring efficient memory utilization. Therefore, integers in this range appear as identical regardless of their actual references.
When using == to compare Integer objects, the reference equality is checked rather than the content. Therefore, if the cached instances are used, the comparison returns true even when the object references are distinct.
This issue does not occur when using the equals() method because it compares the values of the objects, disregarding the references. As a result, it is recommended to use equals() for comparison of Integer objects, especially when the range of values may extend beyond the cached range or when precise equality determination is required.
The above is the detailed content of Why Does `==` Sometimes Fail to Compare `Integer` Objects Correctly in Java?. 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.