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
🌐
Coderanch
coderanch.com › t › 701009 › java › compare-primitive-data-types
How to compare two primitive data types? (Beginning Java forum at Coderanch)
Remember that most attempts to use a short will see it converted to an int on spec. And it is only warp when you're on NCC‑1701; everybody else calls it ... jason edwardes wrote:My guess you can only use equals() to compair objects That's right; primitives don't have methods, only objects (classes). Here the Java naming convention will help you: Classes start with an uppercase letter and primitives with a lowercase letter.
🌐
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 class is an Object while an int is a primitive type. 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.
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › EXP03-J.+Do+not+use+the+equality+operators+when+comparing+values+of+boxed+primitives
EXP03-J. Do not use the equality operators when comparing values of boxed primitives - SEI CERT Oracle Coding Standard for Java - Confluence
Note that primitive integers are also accepted by this declaration because they are autoboxed at the call site. This compliant solution uses the comparison operators, <, >, <=, or >=, because these cause automatic unboxing of the primitive values. The == and != operators should not be used ...
🌐
LabEx
labex.io › tutorials › java-how-to-compare-primitive-integers-in-java-418181
How to compare primitive integers in Java | LabEx
This tutorial provides developers with comprehensive insights into various methods and techniques for comparing integer values efficiently and accurately in Java, covering essential comparison strategies that are crucial for writing robust and reliable code. In Java, primitive integers are ...
🌐
Rip Tutorial
riptutorial.com › pitfall: using == to compare primitive wrappers objects such as integer
Java Language Tutorial => Pitfall: using == to compare primitive...
(This pitfall applies equally to all primitive wrapper types, but we will illustrate it for Integer and int.) When working with Integer objects, it is tempting to use == to compare values, because that is what you would do with int values.
Find elsewhere
🌐
Belief Driven Design
belief-driven-design.com › equality-and-comparison-in-java-a5e0f05b808
Equality and Comparison in Java: Pitfalls and Best Practices | belief driven design
The same problems of floating-point ... To compare object types we need to implement the interface java.lang.Comparable<T> with its single method int compareTo(T)....
🌐
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....
🌐
Delft Stack
delftstack.com › home › howto › java › java compare integer
How to Compare Two Integers in Java | Delft Stack
February 12, 2024 - This method ensures a clear and nuanced understanding of the relationship between primitive integers, contributing to effective decision-making in Java programs. ... This output effectively demonstrates the power of the Integer.compare method in providing a concise and nuanced result for comparing ...
Top answer
1 of 2
26

In general the equality operator in Java performs a so called shallow comparison. In other words it compares the values that variables contains. Now the variables of primitive data types contains the value itself while the reference types contains reference to heap area which stores the actual content. That means that in your code snippet int b will hold value 1 and Integer a will hold the memory address of the actual Integer object on the heap.

Now in the particular example provided by you there is one pecularity. Integer class a special wrapper class that wraps primitive integer types. The compiler can automatically convert between such wrapper objects and primitive types (which is called boxing and unboxing).

Let's walk you code step by step tgo make it clear.

Integer a = 1;

The compiler actually substitue the following code insted of this line:

Integer a = Integer.valueOf(1);

The static method valueOf returns an wrapper object instance that wraps the provided primitive value. This procedure when the compiler constructs a wrapper class out of a primitive type is called boxing.

When using such a wrapper object is compared with with a primitive variable using equality operator

a == b

the compiler actually changes it to the following:

a.intValue() == b;

where intValue returns the primitive value wrapped by the wrapper object (which is called unboxing) i.e. it unboxes the primitive value and make the expression equivalent to comparing two primitives. This is why the equality operator then returned true

2 of 2
5

In your particular example boxed type Integer will be unboxed to a primitive type int and == will compare primitives (i.e. true in your case).

🌐
KapreSoft
kapresoft.com › java › 2023 › 11 › 07 › java-int-vs-integer-best-practices.html
Java • Primitive int vs Integer Best Practices | KapreSoft
November 7, 2023 - While the Integer class has its ... int primitiveInt = 100; // Directly stores the value · Using a primitive int consumes less memory compared to an Integer ......
🌐
Cubussapiens
cubussapiens.hu › 2012 › 05 › java-primitive-type-comparison-a-wat-look
Java primitive type comparison – A wat look – cubussapiens.hu
Sadly, the result is that there is no reasonable way to compare longs and integers by value. However, we found a truly nice wat moment, almost fitting to the issues presented by Gary Bernhardt in a talk called WAT at CodeMash 2012 (or instead of the video, you could have a look at http://www.shopify.com/technology/5370262-wat-a-funny-look-at-ruby-and-javascript-oddities).
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Use boxing with care
* * When both items are of the same type : * 2 primitives 2 objects * ---------------------------------------------------------- * == : value identity * equals() : not applicable value * * * When one item is a primitive, and the other is an object : * == : treat as two primitives * x.equals(y) : treat as two objects; do not compile if x is primitive */ intYear = 1880; integerYear = Integer.valueOf(1880); if (intYear == integerYear){ log("intYear == integerYear: TRUE"); // yes } else { log("intYear == integerYear : FALSE"); } if (integerYear.equals(intYear)){ log("integerYear.equals(intYear): T
🌐
Baeldung
baeldung.com › home › java › core java › comparing objects in java
Comparing Objects in Java | Baeldung
October 10, 2025 - For primitive types, being the same means having equal values: ... Thanks to auto-unboxing, this also works when comparing a primitive value with its wrapper type counterpart: Integer a = new Integer(1); assertThat(1 == a).isTrue();
🌐
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
🌐
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 post shares an observation on Integer Comparisons in Java that I noticed during one coding session. As is well-known, when comparing two integer variables of primitive type in Java, we can simply use the “==” operator.
🌐
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 ...