Backend Development 5 min read

Why Comparing Integer Objects with == Fails for Values Outside the Cache Range in Java

The article explains a junior developer's intermittent bug when adding a new order status with code 200, showing that Java's Integer cache and the use of == instead of equals cause the status check to fail, and provides the correct solution.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Why Comparing Integer Objects with == Fails for Values Outside the Cache Range in Java

Junior developer Xiao Zhang added a new order status called "Feedback" with a numeric code of 200 to an existing order‑status printing module, expecting the system to output "订单已反馈" (order feedback) just like the other statuses.

The original method used simple if (status == 0) { … } and if (status == 2) { … } checks, which worked for the existing statuses.

After introducing the new status, Zhang also created an enum StatusEnum that defines the codes and descriptions, including FEEDBACK(200, "已反馈") , and updated the printing logic to compare the incoming Integer with the enum codes.

Full source code:

public void printOrderStatus(Integer status) {
    if (status == 0) {
        System.out.println("订单已创建");
    }
    if (status == 2) {
        System.out.println("订单已配送");
    }
}

package com.sample.core.basic;

enum StatusEnum {
    ORDERED(0, "已下单"),
    PAYED(1, "已支付"),
    DELIVERED(2, "已配送"),
    FINISHED(3, "已完成"),
    FEEDBACK(200, "已反馈");
    private Integer code;
    private String desc;
    StatusEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }
    public Integer getCode() { return code; }
    public String getDesc() { return desc; }
}

public class IntegerCacheExample {
    public static void main(String[] args) {
        IntegerCacheExample example = new IntegerCacheExample();
        example.printOrderStatus(200);
    }
    public void printOrderStatus(Integer status) {
        if (status == StatusEnum.ORDERED.getCode()) {
            System.out.println("订单已创建");
        }
        if (status == StatusEnum.DELIVERED.getCode()) {
            System.out.println("订单已配送");
        }
        if (status == StatusEnum.FEEDBACK.getCode()) {
            System.out.println("订单已反馈");
        }
    }
}

When the program is run, nothing is printed for the feedback status, even though the other statuses work correctly.

Senior engineer Lao Wang points out that Java caches Integer objects only for values between -128 and 127. Values outside this range, such as 200, are distinct objects, so the expression status == StatusEnum.FEEDBACK.getCode() compares object references and returns false. Using status.equals(StatusEnum.FEEDBACK.getCode()) or unboxing to int resolves the issue.

The case illustrates a subtle pitfall in Java programming: relying on == for object equality can lead to bugs when the objects are not interned, emphasizing the importance of understanding language specifics and using proper equality checks.

BackendJavaenuminteger caching== vs equals
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.