Copyi1 == i2

results in un-boxing and a regular int comparison is done. (see first point in JLS 5.6.2)

Copyi2 == i3 

results in reference comparsion. Remember, i2 and i3 are two different objects. (see JLS 15.21.3)

Answer from rocketboy on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-integer-compare-method
Java Integer compare() method - GeeksforGeeks
December 5, 2018 - // Java program to demonstrate working // of java.lang.Integer.compare() method import java.lang.Integer; class Gfg { // driver code public static void main(String args[]) { int a = 10; int b = 20; // as 10 less than 20, Output will be a value ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-integer-compare-method
Java Integer compare() method
Integer compare() Method The compare() method in the Integer class is a part of the Java interface and is used to compare two integers. It provides a way to compare two Integer objects or primitive int values and determine their relative ordering. Th
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ int-vs-Integer-java-difference-comparison-primitive-object-types
Integer vs. int: What's the difference?
The Integer is compared with .equals while the int uses two equal signs, == . The Integer class is a wrapper class for the int, while the int is not a class at all. The Integer class allows conversion to float, double, long and short, while ...
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2010 โ€บ 10 โ€บ what-is-problem-while-using-in.html
Why Avoid Comparing Integers in Java using == Operator? Integer Cache Example
This happens because autoboxing uses Integer.valueOf() method to convert Integer to int and since method caches Integer object for range -128 to 127, it returns same Integer object in heap, and that's why == operator return true if you compare ...
๐ŸŒ
W3Docs
w3docs.com โ€บ java
How can I properly compare two Integers in Java?
Integer num1 = new Integer(5); ... } ... This code compares the primitive int values of the num1 and num2 objects using the == operator, and the message "The numbers are equal." is printed to the console....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-integer-compareto-method
Java Integer compareTo() method - GeeksforGeeks
April 3, 2023 - Example 02 :The working of compareTo() method in Java to compare two integer values. Java ยท import java.io.*; class GFG { public static void main(String[] args) { Integer num1 = 10; Integer num2 = 5; Integer num3 = 10; int result1 = ...
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java compare integer
How to Compare Two Integers in Java | Delft Stack
February 12, 2024 - When working with Integer objects, use the compareTo method for comparisons. It provides a clear indication of whether one integer is less than, equal to, or greater than another.
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.

๐ŸŒ
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 - Integer values in Java can be compared by utilizing methods available to the wrapper classes that contain them, such as 'compareTo.' Look at examples of the code, and learn how to use them in conjunction with if/then/else statements.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-java-integer-compare-method-117698
Java Integer Compare Method
int val3 = 5; int val4 = 5; int result = Integer.compare(val3, val4); Print the value of the result variable using the System.out.println() method. ... Test the comparison by running the code.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 416799 โ€บ java โ€บ Integer-int
Integer VS int (Beginning Java forum at Coderanch)
You're comparing two Integer object references, not two int (primitive type) values. You want to use compareTo() when attempting to find out whether or not two Integer values are the same.
๐ŸŒ
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
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java integer equals method
Java Integer equals Method
September 1, 2008 - Learn how to use the equals method in Java Integer class to compare integer values effectively. Understand its functionality with examples.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-integer-compare
Java Integer.compare() - Compare two integer values
May 4, 2023 - In this Java tutorial, you will learn about Integer.compare() method, and how to use this method to compare two integer values, with the help of examples. Integer.compare() compares two int values numerically and returns an integer value. If x>y ...
๐ŸŒ
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).
๐ŸŒ
Medium
yogeshbali.medium.com โ€บ important-considerations-for-integer-value-comparison-in-java-d90b25bf29bb
Important Considerations for Integer Value Comparison in Java
April 27, 2025 - As is well-known, when comparing two integer variables of primitive type in Java, we can simply use the โ€œ==โ€ operator. However, there are instances where we may define integer values using variables of the Integer class data type (Integer ...
๐ŸŒ
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 - Wrapper classes provide a way to use primitive data types ( int , boolean , etc..) as objects. Its like wrapping a simple gift (a primitive data type like int) in a fancy decorative wrapper paper (like Integer) so it can be used in more flexible and powerful ways. Imagine you have two copies of the same book. In Java, the == operator checks if the two objects (here, the two books) being compared are the exact same object.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-integer-compare-method
Java Integer compare() Method
Comparing user entered numbers using Scanner class and java.lang.Integer.compare() method. import java.util.Scanner; public class JavaExample{ public static void main(String[] args){ int x, y; Scanner scan = new Scanner(System.in); System.out.print("Enter first int number: "); x = scan.nextInt(); ...