Understanding Integer Comparison in Java: Reference vs Value

Source Code

public class IntComparisonDemo {
    public static void main(String[] args) {
        
        Integer first = 100;
        Integer second = 100;
        System.out.println("Integer first = 100;");
        System.out.println("Integer second = 100;");
        System.out.println("first == second ? " + (first == second));
        // Result: true

        Integer third = 200;
        Integer fourth = 200;
        System.out.println("\nInteger third = 200;");
        System.out.println("Integer fourth = 200;");
        System.out.println("third == fourth ? " + (third == fourth));
        // Result: false

        Integer fifth = 100;
        Integer sixth = new Integer(100);
        System.out.println("\nInteger fifth = 100;");
        System.out.println("Integer sixth = new Integer(100);");
        System.out.println("fifth == sixth ? " + (fifth == sixth));
        // Result: false

        Integer seventh = new Integer(100);
        Integer eighth = new Integer(100);
        System.out.println("\nInteger seventh = new Integer(100);");
        System.out.println("Integer eighth = new Integer(100);");
        System.out.println("seventh == eighth ? " + (seventh == eighth));
        // Result: false

        Integer ninth = 200;
        int tenth = 200;
        System.out.println("\nInteger ninth = 200;");
        System.out.println("int tenth = 200;");
        System.out.println("ninth == tenth ? " + (ninth == tenth));
        // Result: true
    }
}

Output

Integer first = 100;
Integer second = 100;
first == second ? true

Integer third = 200;
Integer fourth = 200;
third == fourth ? false

Integer fifth = 100;
Integer sixth = new Integer(100);
fifth == sixth ? false

Integer seventh = new Integer(100);
Integer eighth = new Integer(100);
seventh == eighth ? false

Integer ninth = 200;
int tenth = 200;
ninth == tenth ? true

Analysis

Why does first == second return true but third == fourth return false?

The Integer.valueOf(int i) method maintains an internal cache for values ranging from -128 to 127. When an Integer object is created within this range through autoboxing, the cached instance is returned rather than constructing a new object. This means both first and second point to the same object reference in memory.

However, when the value falls outside this range, valueOf creates a fresh object each time, resulting in distinct references. Therefore, third and fourth reference different objects, causing the comparison to fail.

Why does fifth == sixth return false?

When using new Integer(100), a brand new object is instantiated on the heap, bypasssing the cache entirely. Meanwhile, fifth retrieves the cached instance. Since they are different objects in memory, the reference comparison yields false.

Why does seventh == eighth return false?

Both seventh and eighth are created via new Integer(100), resulting in two separate object instances on the heap. Reference comparison therefore returns false.

Why does ninth == tenth return true?

When comparing a boxed Integer with a primitive int, the JVM performs autoboxing conversion on the Integer object first. This unboxing transforms the object into its primitive equivalent before comparison. Consequently, the comparison operates on actual numeric values rather than object references, returning true when the values match.

Key Takeaways

Scenario Comparison Ressult Reason
Integer within cache range (-128 to 127) == returns true Same cached instance
Integer outside cache range == returns false Different object instances
new Integer() compared with cache reference == returns false Different object instances
Integer compared with int == returns true Auto-unboxing compares values

Best Practice

When comparing two Integer objects, always use equals() instead of == to insure value-based comparison:

Integer a = 200;
Integer b = 200;

// Correct way
System.out.println(a.equals(b));  // true

// Potential bug
System.out.println(a == b);      // false

For int comparisons with Integer, using == is safe due to automatic unboxing.

Tags: java integer autoboxing comparison Cache

Posted on Fri, 30 Oct 2026 16:27:46 +0000 by Bunyip