70

Is it better to write

int primitive1 = 3, primitive2 = 4;
Integer a = new Integer(primitive1);
Integer b = new Integer(primitive2);
int compare = a.compareTo(b);

or

int primitive1 = 3, primitive2 = 4;
int compare = (primitive1 > primitive2) ? 1 : 0;
if(compare == 0){
    compare = (primitive1 == primitive2) ? 0 : -1;
}

I think the second one is better, should be faster and more memory optimized. But aren't they equal?

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • 13
    never call "new Integer" instead using Integer.valueOf(int) (http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#valueOf(int)) this method accesses an internal cache and will often avoid an allocation for small integers. – luke Feb 05 '12 at 15:35
  • 2
    FYI: On modern JVMs (like HotSpot), the two programs will probably become the same machine code after optimization. The best way to know for sure is (as always) benchmark both solutions. – Adam Paynter Feb 05 '12 at 15:35
  • 2
    In addition to @luke 's comment: new Integer() will be deprecated in Java9 http://download.java.net/java/jdk9/docs/api/java/lang/Integer.html#Integer-int- – Naxos84 Oct 27 '16 at 07:11
  • 1
    https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#Integer-int- is a working link – MartyIX Jan 18 '19 at 14:18

8 Answers8

147

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.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
67

Use Integer.compare(int, int). And don'try to micro-optimize your code unless you can prove that you have a performance issue.

MForster
  • 8,806
  • 5
  • 28
  • 32
15

May I propose a third

((Integer) a).compareTo(b)  
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
5

Wrapping int primitive into Integer object will cost you some memory, but the difference will be only significant in very rare(memory demand) cases (array with 1000+ elements). I will not recommend using new Integer(int a) constructor this way. This will suffice :

Integer a = 3; 

About comparision there is Math.signum(double d).

compare= (int) Math.signum(a-b); 
4

They're already ints. Why not just use subtraction?

compare = a - b;

Note that Integer.compareTo() doesn't necessarily return only -1, 0 or 1 either.

MQDuck
  • 423
  • 1
  • 3
  • 8
  • 7
    This will produce erroneous results in the case of overflow. Try `a = Integer.MAX_VALUE` and `b = -3`. The result will be negative, indicating that `a < b`. – Jim Mischel Oct 03 '16 at 12:43
4

For pre 1.7 i would say an equivalent to Integer.compare(x, y) is:

Integer.valueOf(x).compareTo(y);
Dave
  • 53
  • 5
3

If you are using java 8, you can create Comparator by this method:

Comparator.comparingInt(i -> i);

if you would like to compare with reversed order:

Comparator.comparingInt(i -> -i);
tanghao
  • 4,073
  • 2
  • 13
  • 17
-1

If you need just logical value (as it almost always is), the following one-liner will help you:

boolean ifIntsEqual = !((Math.max(a,b) - Math.min(a, b)) > 0);

And it works even in Java 1.5+, maybe even in 1.1 (i don't have one). Please tell us, if you can test it in 1.5-.

This one will do too:

boolean ifIntsEqual = !((Math.abs(a-b)) > 0);
WebComer
  • 1,131
  • 2
  • 19
  • 31
  • These expressions perform a lot of wasted computation to (in the first case, incorrectly) compute a == b, which is not what the question is about. – Clement Cherlin Jun 09 '20 at 13:50