Home Java Integer Methods equals() Java Integer equals() Method Definition In Java, a.equals(o) returns true when o is an Integer object holding the same int value as a. Otherwise it returns false.
Use it instead of == when you compare two Integer objects. The == operator compares object references, which only works by accident for small numbers.
An instance method of java.lang.Integer: call it on an Integer object. Integer lives in java.lang, so no import is needed.
Syntax Call it on an Integer and pass any object.
Integer a = 1000;
Integer b = 1000;
boolean same = a.equals(b); // true
boolean ref = (a == b); // false: two objects Call What it does a.equals(b)true if b is an Integer with the same value.a.equals(5)The 5 is boxed to an Integer first, so this works. a == bCompares references, not numbers (avoid for Integer objects). Objects.equals(a, b)A null-safe version.
If one side is a primitive int, == unboxes the object and compares numbers, so a == 1000 is fine. The trouble starts only when both sides are Integer objects.
Library declarations // java.lang.Integer
public boolean equals(Object obj) Parameters Parameter Description obj (Object)The object to compare with. It can be null, which gives false.
Return Value Return Value Description trueobj is an Integer with the same int value.falseobj is null, a different value, or any other type, including Long.
Important
The types must match. Integer.valueOf(5).equals(5L) is false because 5L boxes to a Long, and equals("5") is false because a String is not an Integer. Calling equals on a null variable throws NullPointerException; use Objects.equals(a, b) when either side may be null.
Compare with Similar Methods Related ways to compare numbers:
Method How it differs compareTo(o)Says which value is larger, not only whether they match. hashCode()Equal Integer objects always have the same hash code. valueOf(i)Explains the cache that makes == work for small values. String.equals()The same idea for text.
Java Examples 1. equals vs == 2. Type matters 3. Search and null safety Example 1 — Compare Integer Objects the Right Way Compare small and large numbers with equals and with ==.
public class IntegerEqualsDemo {
public static void main(String[] args) {
Integer small1 = 100;
Integer small2 = 100;
System.out.println("100 equals 100: " + small1.equals(small2));
System.out.println("100 == 100: " + (small1 == small2));
Integer big1 = 1000;
Integer big2 = 1000;
System.out.println("1000 equals 1000: " + big1.equals(big2));
System.out.println("1000 == 1000: " + (big1 == big2));
System.out.println("1000 == int 1000: " + (big1 == 1000));
}
} 100 equals 100: true
100 == 100: true
1000 equals 1000: true
1000 == 1000: false
1000 == int 1000: true How It Works equals reads the numbers. It returns true for both pairs because the values match.Small numbers share objects. Autoboxing reuses cached objects for -128 to 127, so 100 == 100 happens to be true.1000 is not cached. Each box is a new object, so == compares two different references and returns false.A primitive fixes it. When one side is a plain int, Java unboxes the object and compares the numbers, so the last line is true.Example 2 — See Why Integer Does Not Equal Long Compare an Integer with other objects and see which comparisons are false.
public class IntegerEqualsTypes {
public static void main(String[] args) {
Integer five = 5;
System.out.println("equals(5): " + five.equals(5));
System.out.println("equals(5L): " + five.equals(5L));
System.out.println("equals(\"5\"): " + five.equals("5"));
System.out.println("equals(null): " + five.equals(null));
System.out.println("long compare: " + (five.longValue() == 5L));
}
} equals(5): true
equals(5L): false
equals("5"): false
equals(null): false
long compare: true How It Works An int boxes to Integer. The argument 5 becomes an Integer, so the first call is true.A long boxes to Long. 5L becomes a Long, which is a different class, so the result is false even though the number matches.Text and null. A String is not an Integer, and null is never equal to an object, so both are false.The fix for mixed types. Convert one side first. longValue() returns a primitive, and == then compares numbers.Example 3 — Search a List and Compare Safely with null Use equals through List.contains and indexOf, and use Objects.equals when a value may be null.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class IntegerEqualsLists {
public static void main(String[] args) {
List<Integer> ids = new ArrayList<>(Arrays.asList(1000, 2000, 3000));
System.out.println("contains 2000: " + ids.contains(2000));
System.out.println("index of 3000: " + ids.indexOf(3000));
System.out.println("contains 4000: " + ids.contains(4000));
Integer missing = null;
System.out.println("null safe: " + Objects.equals(missing, 1000));
System.out.println("both null: " + Objects.equals(missing, null));
}
} contains 2000: true
index of 3000: 2
contains 4000: false
null safe: false
both null: true How It Works Lists use equals. contains and indexOf call equals on each element, so the separate Integer objects for 2000 match.The position. 3000 sits at index 2, and 4000 is not in the list at all.A null variable. Calling missing.equals(1000) would throw, but Objects.equals checks for null first and returns false.Two nulls are equal. Objects.equals(null, null) is true.Frequently Asked Questions How do I compare two Integer objects in Java? Use a.equals(b), or compare after unboxing with a.intValue() == b.intValue(). Do not use == on two Integer objects.
Why does Integer == work for 100 but not for 1000? Java caches Integer objects for -128 to 127, so those values share one object. Larger values create new objects, and == compares references.
Why is Integer.equals(Long) false for the same number? equals checks the class too. A Long is not an Integer, so the answer is false. Convert both to long or int first.
What happens if I call equals on a null Integer? It throws NullPointerException. Use Objects.equals(a, b) when either side may be null.
Is it fine to compare an Integer with an int using ==? Yes. Java unboxes the Integer and compares the numbers. It only fails when both sides are Integer objects.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion