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 OverflowFor 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.
Use Integer.compare(int, int). And don'try to micro-optimize your code unless you can prove that you have a performance issue.
int is a primitive. You can use the wrapper Integer like
Integer first_int = 1;
Integer second_int = 1;
if(first_int.equals(second_int)){ // <-- Integer is a wrapper.
or you can compare by value (since it is a primitive type) like
int first_int = 1;
int second_int = 1;
if(first_int == second_int){ // <-- int is a primitive.
JLS-4.1. The Kinds of Types and Values says (in part)
There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).
If you want to compare between
1-two integer
If(5==5)
2- char
If('m'=='M')
3 string
String word="word"
word.equals("word")
Videos
Java uses the concept of Unboxing when you compare primitive and wrapper class. Where in a Integer variable is translated into primitive int type.
Following is what is happening with your code:
Integer a = 5; //a of type Integer i.e. wrapper class
int b = 5; //b of primitive int type
System.out.println(a==b) // a is unboxed to int type and compared with b, hence true
For more on Autoboxing(reverse of Unboxing) and Unboxing this link.
The correct answer is due to unboxing, but let's be more explicit here.
The rules for numerical equivalence are described in the Java Language Specification, specifically these rules:
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).
Since Integer is convertible to a numeric type (per these rules), the values you're comparing become semantically equivalent to a.intValue() == b.
It should be stressed that this conversion will fail if a is null; that is, you will get a NullPointerException when attempting to do that equivalence check.
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
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).
In Java 7, static int compare for primitive types have been added to all primitive object wrapper classes, i.e there is now:
java.lang.Integer: static int compare( int x, int y );
java.lang.Byte: static int compare( byte x, byte y );
java.lang.Short: static int compare( short x, short y );
etc...
- For
int, write your owncomparemethod (it requires at most three lines of code). - For
double, useDouble.compare(not to be confused withcompareTo). - For
float, useFloat.compare.
The last two take primitive types and thus avoid boxing and unboxing. I can see an argument for Integer providing a similar compare method, but as things stand it doesn't.