Comparing two boxed primitive values using == or != compares object identity, which may not be intended.

Usually, you should compare non-primitive objects, for example boxed primitive values, by using their equals methods.

With the following definition, the method call refEq(new Integer(2), new Integer(2)) returns false because the objects are not identical.

With the following definition, the method call realEq(new Integer(2), new Integer(2)) returns true because the objects contain equal values.

  • J. Bloch and N. Gafter, Java Puzzlers: Traps, Pitfalls, and Corner Cases, Puzzle 32. Addison-Wesley, 2005.
  • Java API Specification: Object.equals(), Integer.equals().