


Why Does Java's `==` Operator Sometimes Compare Integer Object Values and Sometimes References?
Surprising Integer Boxing Behavior in Java
In Java, an Integer object is a wrapper for a primitive int value. When boxing an int into an Integer, one would typically expect the == operator to compare the values of the objects. However, in certain cases, the == operator instead compares the references of the objects. This behavior can lead to surprising results.
Consider the following code:
public class Scratch { public static void main(String[] args) { Integer a = 1000, b = 1000; System.out.println(a == b); Integer c = 100, d = 100; System.out.println(c == d); } }
When run, this code prints:
false true
The first output is as expected: == compares the references of two distinct Integer objects. However, the second output is surprising. Why does == return true when c and d have the same value?
The answer lies in the Java Language Specification (JLS). According to Section 5.1.7 of the JLS:
If the value p being boxed is true, false, a byte, a char in the range u0000 to u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
This means that Integer objects created from primitive values within the specified ranges are guaranteed to have identical references. In our case, c and d are both created from the same int value, 100, which falls within the range of -128 to 127. Therefore, they have the same reference, and the == operator returns true.
While the behavior of the second line of output is guaranteed, the JLS suggests that the behavior of the first line of output is not. In theory, a Java implementation could cache Integer objects for common values to improve performance, but the JLS does not require this behavior. As a result, different Java implementations may handle this case differently.
The above is the detailed content of Why Does Java's `==` Operator Sometimes Compare Integer Object Values and Sometimes References?. 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. ...

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...

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...

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...

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

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...

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...
