For performance, it usually best to make the code as simple and clear as possible and this will often perform well (as the JIT will optimise this code best). In your case, the simplest examples are also likely to be the fastest.


I would do either

int cmp = a > b ? +1 : a < b ? -1 : 0;

or a longer version

int cmp;
if (a > b)
   cmp = +1;
else if (a < b)
   cmp = -1;
else
   cmp = 0;

or

int cmp = Integer.compare(a, b); // in Java 7
int cmp = Double.compare(a, b); // before Java 7

It's best not to create an object if you don't need to.

Performance wise, the first is best.

If you know for sure that you won't get an overflow you can use

int cmp = a - b; // if you know there wont be an overflow.

you won't get faster than this.

Answer from Peter Lawrey on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-integer-compareto-method
Java Integer compareTo() method - GeeksforGeeks
April 3, 2023 - public int compareTo(Integer anotherInt) Parameter : anotherInt- : the Integer to be compared. Return : - This method returns the value 0 if this Integer is equal to the argument Integer; - a value less than 0 if this Integer is numerically ...
🌐
TutorialsPoint
tutorialspoint.com › java-integer-compareto-method
Java Integer compareTo() method
public class Main { public static void main(String[] args) { Integer obj1 = new Integer("100"); Integer obj2 = new Integer("200"); int res = obj1.compareTo(obj2); if(retval > 0) { System.out.println("obj1 is greater than obj2"); } else if(retval < 0) { System.out.println("obj1 is less than ...
🌐
Zero To Mastery
zerotomastery.io › blog › java-compareto-method
Beginner's Guide To compareto In Java (With Code Examples) | Zero To Mastery
Here’s how to define sorting ... { this.name = name; this.age = age; } @Override public int compareTo(Person other) { return Integer.compare(this.age, other.age); // Sort by age } }...
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › integer compareto in java
Integer compareTo in Java
September 1, 2008 - Following is the declaration for java.lang.Integer.compareTo() method ... This method 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 argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer. ... The following example shows the usage of Integer compareTo() method to compare two integer objects.
🌐
Baeldung
baeldung.com › home › java › core java › guide to implementing the compareto method
Guide to Implementing the compareTo Method - Java
May 29, 2025 - Let’s look at an example of a custom class that compares players based on the number of goals they have scored: @Override public int compareTo(FootballPlayer anotherPlayer) { return Integer.compare(this.goalsScored, anotherPlayer.goalsScored); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-compareto-method-with-examples
Java String compareTo() Method with Examples - GeeksforGeeks
January 20, 2025 - Example: Below is the implementation of int compareToIgnoreCase(String str): Java ·
🌐
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 ...
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_string_compareto.asp
Java String compareTo() Method
Java Examples Java Videos Java ... Plan Java Interview Q&A Java Certificate ... String myStr1 = "Hello"; String myStr2 = "Hello"; System.out.println(myStr1.compareTo(myStr2)); // Returns 0 because they are equal...
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 › home › java › java number compareto method
Java Number compareTo Method
September 1, 2008 - However, two different types cannot be compared, both the argument and the Number object invoking the method should be of the same type. public int compareTo( NumberSubClass referenceName )
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Comparable.html
Comparable (Java Platform SE 8 )
October 20, 2025 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
🌐
Reddit
reddit.com › r/javahelp › understanding the compareto() method
r/javahelp on Reddit: Understanding the compareTo() method
August 8, 2020 -

From what I have been reading, the compareTo() method returns the difference of the Unicode numerical values of two Strings when they are compared with each other. For instance, the String "hello" when compared with the String "hello" returns an integer value of zero, since they both have exactly the same Unicode characters in them. Based on my understanding of this method, "hello" should return zero when compared to "olleh", because the two Strings have the exact same Unicode characters in them. Instead, though, I am getting integer value of 7 returned to the console. Can someone break this down a bit for me to help me understand it better? Thanks in advance. Here is my code:

String str1 = "hello";
String str2 = "olleh";
System.out.println(str1.compareTo(str2)); // 7

🌐
BeginnersBook -
beginnersbook.com › home › java › java integer compareto() method
Java Integer compareTo() Method
October 23, 2022 - ... public class JavaExample{ public static void main(String[] args){ Integer obj = new Integer(55); int result = obj.compareTo(34); int result2 = obj.compareTo(55); int result3 = obj.compareTo(89); System.out.println(result); //55 > 34 so 1 System.out.println(result2); //55 == 55 so 0 ...
🌐
Jenkov
jenkov.com › tutorials › java-collections › comparable.html
Java Comparable
The Java Integer class implements the Comparable interface, so you can call compareTo() Here is an example:
🌐
Scaler
scaler.com › home › topics › java string compareto() method
Java String compareTo() Method with Examples - Scaler Topics
May 10, 2023 - It returns a positive integer if string1 is lexicographically greater than string2, negative if string2 is greater than string1, and zero if both are equal. The compareTo() method in Java throws two Exceptions.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-compareto-method-example
Java String compareTo() Method with examples
... public class JavaExample { public static void main(String args[]) { String str1 = "Cow"; //This is an empty string String str2 = ""; String str3 = "Goat"; int lenOfStr1 = str1.compareTo(str2); int lenOfStr3 = str3.compareTo(str2); System.out.println("Length of string str1: "+lenOfStr1); ...
🌐
Stack Overflow
stackoverflow.com › questions › 72160928 › how-to-implement-compareto-method-in-java-and-what-does-it-mean
comparable - How to implement compareTo method in Java and what does it mean - Stack Overflow
It's important to me to understand programming and not just memorizing it. For example, in the following code we have an int variable spaceshipClassComparison, which is equal to this.spaceshipClass.compareTo(other.spaceshipClass).
🌐
LabEx
labex.io › tutorials › java-how-to-use-compareto-method-to-compare-java-objects-414156
How to use compareTo() method to compare Java objects | LabEx
The implementation of the compareTo() method should return an integer value that indicates the relative order of the current object and the argument object. The possible return values are: A negative integer if the current object is less than the argument object · Zero if the current object is equal to the argument object · A positive integer if the current object is greater than the argument object · Here's an example of how you can implement the compareTo() method in the Person class:
🌐
Studytonight
studytonight.com › java-wrapper-class › java-integer-compareto-method
Java Integer compareTo() Method - Studytonight
0,-1,1 depending upon the value ... static void main(String[] args) { Integer ob1 = 100; Integer ob2 = 200; Integer ob3 = 300; Integer ob4 = 100; System.out.println(ob1.compareTo(ob2)); //Output will be less than zero ...