This is one of the less pleasant effects of auto-boxing.
In your first example, the ==
operator indicates identity equality: the two objects will only be equal if they are the same instance.
In your second example, the '>=' operator indicates numeric comparison: the two objects will be auto-unboxed and then compared.
Making things more confusing, there's a range of "small" integers (-128 <= X <= 127, iirc) for which the JVM caches Integer
values, so the ==
operator sometimes works.
Bottom line: use .equals()
and .compareTo()
.