1

To play with autoboxing and understand it better, I developed this piece of code in JAVA:

public class Autoboxing {

public static void cmp(Integer a, Integer b) {
    if (a < b) System.out.printf("%d < %d\n", a, b);
    else if (a == b) System.out.printf("%d == %d\n", a, b);
    else System.out.printf("%d > %d\n", a, b);
}

public static void main (String[] args) {
    cmp(new Integer(42), new Integer(42));
    cmp(42, 42);
    cmp(128, 128);

}
}

I would expect the result to be:

42 > 42
42 == 42
128 == 128

But the actual result differs for the last line. It gives me:

128 > 128

What exactly is going on here, so that the primitive 42 is behaving like I would expect (giving me the equals result), but the primitive 128 is not?

embiem
  • 113
  • 5
  • you're right. [this answers my question](http://stackoverflow.com/questions/10149959/using-operator-in-java-to-compare-wrapper-objects/10149985#10149985) – embiem Jul 21 '15 at 18:47

0 Answers0