No, == between Integer, Long etc will check for reference equality - i.e.

Integer x = ...;
Integer y = ...;

System.out.println(x == y);

this will check whether x and y refer to the same object rather than equal objects.

So

Integer x = new Integer(10);
Integer y = new Integer(10);

System.out.println(x == y);

is guaranteed to print false. Interning of "small" autoboxed values can lead to tricky results:

Integer x = 10;
Integer y = 10;

System.out.println(x == y);

This will print true, due to the rules of boxing (JLS section 5.1.7). It's still reference equality being used, but the references genuinely are equal.

If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

Personally I'd use:

if (x.intValue() == y.intValue())

or

if (x.equals(y))

As you say, for any comparison between a wrapper type (Integer, Long etc) and a numeric type (int, long etc) the wrapper type value is unboxed and the test is applied to the primitive values involved.

This occurs as part of binary numeric promotion (JLS section 5.6.2). Look at each individual operator's documentation to see whether it's applied. For example, from the docs for == and != (JLS 15.21.1):

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).

and for <, <=, > and >= (JLS 15.20.1)

The type of each of the operands of a numerical comparison operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs. Binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then signed integer comparison is performed; if this promoted type is float or double, then floating-point comparison is performed.

Note how none of this is considered as part of the situation where neither type is a numeric type.

Answer from Jon Skeet on Stack Overflow
Top answer
1 of 11
414

No, == between Integer, Long etc will check for reference equality - i.e.

Integer x = ...;
Integer y = ...;

System.out.println(x == y);

this will check whether x and y refer to the same object rather than equal objects.

So

Integer x = new Integer(10);
Integer y = new Integer(10);

System.out.println(x == y);

is guaranteed to print false. Interning of "small" autoboxed values can lead to tricky results:

Integer x = 10;
Integer y = 10;

System.out.println(x == y);

This will print true, due to the rules of boxing (JLS section 5.1.7). It's still reference equality being used, but the references genuinely are equal.

If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

Personally I'd use:

if (x.intValue() == y.intValue())

or

if (x.equals(y))

As you say, for any comparison between a wrapper type (Integer, Long etc) and a numeric type (int, long etc) the wrapper type value is unboxed and the test is applied to the primitive values involved.

This occurs as part of binary numeric promotion (JLS section 5.6.2). Look at each individual operator's documentation to see whether it's applied. For example, from the docs for == and != (JLS 15.21.1):

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).

and for <, <=, > and >= (JLS 15.20.1)

The type of each of the operands of a numerical comparison operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs. Binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then signed integer comparison is performed; if this promoted type is float or double, then floating-point comparison is performed.

Note how none of this is considered as part of the situation where neither type is a numeric type.

2 of 11
51

Since Java 1.7 you can use Objects.equals:

java.util.Objects.equals(oneInteger, anotherInteger);

Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

🌐
TutorialsPoint
tutorialspoint.com › check-two-numbers-for-equality-in-java
Check two numbers for equality in Java
To check two numbers for equality in Java, we can use the Equals() method as well as the == operator. Firstly, let us set Integers. Integer val1 = new Integer(5); Integer val2 = new Integer(5); Now, to check whether they are equal or not, let us use the == operator.
🌐
Delft Stack
delftstack.com › home › howto › java › java compare integer
How to Compare Two Integers in Java | Delft Stack
February 12, 2024 - To assess the result of the equals method, we employ an if-else construct. If the result is true, it implies that num1 is equal to num2, leading to the output num1 is equal to num2.
🌐
W3Docs
w3docs.com › java
How can I properly compare two Integers in Java?
To compare two Integer objects in Java, you can use the equals() method. The equals() method compares the value of the two objects and returns true if they are equal, or false if they are not.
🌐
Quora
quora.com › Can-we-compare-integers-by-using-equals-in-Java
Can we compare integers by using equals() in Java? - Quora
Answer (1 of 7): To sum up what others have provided by examples: * For reference types (Integer), use equals to compare the values of two Integers and == to compare whether the references point to the same object (i.e., share the same address).
🌐
w3resource
w3resource.com › java-exercises › basic › java-basic-exercise-32.php
Java - Compare two numbers
Next it performs a series of comparisons using if statements and prints the results using System.out.printf(): if (number1 == number2) checks if 'number1' is equal to 'number2' and prints a message if they are equal.
🌐
TutorialsPoint
tutorialspoint.com › java-integer-compare-method
Java Integer compare() method
The Integer.compare() method is a simple and effective way to compare integers in Java. It is especially useful in sorting and when working with collections. The method provides clear results: a negative value if the first integer is smaller, zero if they are equal, and a positive value if ...
🌐
TutorialKart
tutorialkart.com › java › java-integer-compare
Java Integer.compare() - Compare two integer values
May 4, 2023 - In this example, we will take int values for x and y such that x and y are equal. When we compare x and y using Integer.compare(x, y) method, we should get a return value of 0, since x and y have same integer value.
Find elsewhere
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
How to Compare Integer Values in Java - Lesson | Study.com
March 26, 2018 - When we create a new instance of the Integer class, each new variable representing that instance is an instance variable. We can use the compareTo() method of this class to compare integer values. Values of -1, 0, and 1 are returned, based on ...
🌐
LabEx
labex.io › tutorials › java-java-integer-equals-method-117708
Mastering the Java Integer Equals Method | LabEx
// ~/project/IntegerEqualsMethod.java ... IntegerEqualsMethod ... In this step, create two different Integer objects with different values and then compare them using the equals() method....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-integer-compare-method
Java Integer compare() method - GeeksforGeeks
December 5, 2018 - The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value ...
🌐
Medium
medium.com › @wanisha2013 › always-use-equals-to-compare-integer-objects-in-java-e35e469332aa
Always use equals() to compare Integer Objects in Java | by Anisha Wadhwani | Medium
January 29, 2025 - In Java, the == operator checks ... books are separate physical copies. On the other hand, the equals() method checks if the content/value of the two objects is the same....
🌐
GeeksforGeeks
geeksforgeeks.org › java › check-if-two-integers-are-equal-or-not-in-java
Check if Two Integers are Equal or Not in Java - GeeksforGeeks
June 21, 2021 - compareTo() method returns 0 if both strings are same, else returns 1 or -1. ... // Check Two Integers are Equal or Not in Java // using String functions import java.io.*; class GFG { public static void main(String[] args) { String firstNumber ...
🌐
LabEx
labex.io › tutorials › java-how-to-display-the-result-of-integer-comparison-in-java-414011
How to display the result of integer comparison in Java | LabEx
In this example, the ternary operator (a < b) ? "a is less than b" : "a is greater than or equal to b" evaluates the comparison a < b and assigns the appropriate string to the result variable. Java also provides various methods in the Integer class that allow you to compare integers.
🌐
Medium
yogeshbali.medium.com › important-considerations-for-integer-value-comparison-in-java-d90b25bf29bb
Important Considerations for Integer Value Comparison in Java
April 27, 2025 - This is because they are distinct objects in memory. To compare two Integer values in Java, it is recommended to use the equals() method or the intValue() method of the Integer class.
🌐
Quora
quora.com › What-is-the-most-efficient-way-to-compare-two-integers-for-equality-in-Java-or-C
What is the most efficient way to compare two integers for equality in Java or C++? - Quora
Answer: Letting the compiler optimize it for you. Let’s assume for a second that writing this: [code]bool test = 3 & 4; [/code]was producing more efficient instructions than this: [code]bool test = 3 == 4; [/code]You may know it and take advantage of it when you remember to do so. The compile...
🌐
Codemia
codemia.io › knowledge-hub › path › how_can_i_properly_compare_two_integers_in_java
How can I properly compare two Integers in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises