Fundamentals 4 min read

Why Java equals Returns False for Different Wrapper Types and How to Avoid It

Although numeric wrapper objects in Java may hold equal values, using the equals method on different wrapper types—or mixing primitive and wrapper types—returns false; this article explains the underlying source‑code behavior and recommends using type‑consistent equals or the compare methods to avoid bugs.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Why Java equals Returns False for Different Wrapper Types and How to Avoid It

In Java, comparing two numeric wrapper objects of different types with equals returns false even when the underlying values are the same.

Example:

public static void main(String[] args) {
    System.out.println(Objects.equals(1L, 1));
    System.out.println(Long.valueOf(1L).equals(1));
    System.out.println(Integer.valueOf(1).equals(1L));
}

The output shows false for each call.

Source analysis reveals why:

public boolean equals(Object obj) {
    if (obj instanceof Long) {
        return value == ((Long)obj).longValue();
    }
    return false;
}

Similarly, java.lang.Integer#equals checks only instanceof Integer before comparing values, returning false for other types.

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}

The utility method java.util.Objects#equals simply delegates to a.equals(b) after a null check, so it inherits the same type‑strict behavior.

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

Therefore, the prerequisite for using equals on numeric wrapper types is that the objects must be of the same wrapper class.

To avoid bugs, either convert the values to the same type before calling equals , or preferably use the dedicated compare methods which handle mixed types correctly.

Example using compare methods:

public static void main(String[] args) {
    System.out.println(Objects.compare(1, 1, Integer::compareTo));
    System.out.println(Objects.compare(1L, 1L, Long::compare));
    int a = 1;
    Long b = 1L;
    System.out.println(Long.compare(a, b));
    System.out.println(Integer.compare(a, Math.toIntExact(b)));
}

The results show correct comparisons regardless of the original types.

Summary: In Java, equals on different numeric wrapper types (or mixed primitive and wrapper) returns false even if values match; ensure type consistency or use compare methods for reliable numeric comparisons.

Programmingobjectswrapperequalscompare
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

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.