Well, have you checked what Integer.compare does? Do you think those two can be used interchangeably after reading what it does? Depending on if your integers are int or Integer and the intialization and/or value comparing them with == doesn't necessarily give the result you'd expect if they are of type Integer, they aren't necessarily autoboxed. Answer from gramdel on reddit.com
🌐
TutorialsPoint
tutorialspoint.com › integer-equals-method-in-java
Integer Equals() method in Java
June 26, 2020 - JavaObject Oriented ProgrammingProgramming · The Equals() method compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object. Let us first set Integer object.
🌐
LabEx
labex.io › tutorials › java-java-integer-compare-method-117698
Java Integer Compare Method
int val1 = 5; int val2 = 10; int result = Integer.compare(val1, val2); Print the value of the result variable using the System.out.println() method. ... Test the comparison by running the code.
🌐
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 ...
🌐
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 less than zero System.out.println(Integer.compare(a, b)); int x = 30; int y = 30; // as 30 equals 30, Output will be zero System.out.println(Integer.compare(x, y)); int w = 15; int z = 8; // as 15 is greater than 8, Output will be a value greater than zero System.out.println(Integer.compare(w, z)); } } Output:
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.

🌐
Hacker News
news.ycombinator.com › item
How to Compare Integer Values in Java? | Hacker News
September 27, 2023 - I propose a new HN vote category, "AI generated", with a lowered death threshold than "flagged", or maybe a new title annotation, so HN readers don't have to read discussion to find out it's dead-horse-flogging, slightly wrong, AI spam text · I guess you are on to something... by the way, ...
Find elsewhere
🌐
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).
🌐
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.
🌐
Oreate AI
oreateai.com › blog › understanding-integer-comparison-in-java-a-friendly-guide › 3269c609e17ee7e1fa55621580bd61de
Understanding Integer Comparison in Java: A Friendly Guide - Oreate AI Blog
January 15, 2026 - Explore how Java's compareTo method simplifies integer comparisons through clear examples and practical applications for better coding practices.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - Compares two int values numerically treating the values as unsigned. ... the value 0 if x == y; a value less than 0 if x < y as unsigned values; and a value greater than 0 if x > y as unsigned values ... Converts the argument to a long by an unsigned conversion.
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
How to Compare Integer Values in Java - Lesson | Study.com
July 2, 2025 - 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.
🌐
Monicagranbois
monicagranbois.com › blog › java › comparing-integers-using-integercompare-vs-subtraction
Comparing integers using Integer.compare vs subtraction
August 4, 2015 - It’s basically a conditional expression that uses comparison operations instead of subtraction: ... In the exercise, string lengths are always non-negative, so subtracting them cannot result in overflow. However, I always recommend using Integer.compare(), so that you don’t have to prove that overflow cannot occur.
🌐
Coderanch
coderanch.com › t › 441470 › java › Compare-String-Integer
Compare String with Integer (Java in General forum at Coderanch)
April 16, 2009 - I.e. String "123" and integer 5 If you compare as integer values, then 5 is less than "123". If you compare as string values, then 5 is greater than "123". “Everything should be as simple as it is, but not simpler.” Albert Einstein ... Please take the time to choose the correct forum for your posts. This forum is for questions on Advanced Java.
🌐
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 two Integer object in the range -128 to 127, but return false afterward, causing a bug. Before Java 1.4 that was not a problem because there was no autoboxing, so you always knew that == will check the reference in heap instead of value comparison.
🌐
TutorialsPoint
tutorialspoint.com › java-integer-compare-method
Java Integer compare() method
January 6, 2025 - 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.
🌐
University of Washington
courses.cs.washington.edu › courses › cse373 › 17sp › hw6 › IntegerComparator.java
https://courses.cs.washington.edu/courses/cse373/1...
package sorting; import java.util.Comparator; /** * This class is for you to use * This class implements a Comparator for Integers. Two Integers are compared by * their numerical values, which follows classic intuition about integer * comparison. The first integer is considered less than the ...
🌐
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...
🌐
W3Schools
w3schools.com › java › java_operators_comparison.asp
Java Comparison Operators
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Comparison operators are used to compare two values (or variables).
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-integer-compareto-method
Java Integer compareTo() method - GeeksforGeeks
April 3, 2023 - The compareTo() method of Integer class of java.lang package compares two Integer objects numerically and returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the ...