以前就知道这个现象,不过没继续追究过。今儿打开Gmail,有封Core Java Techologies Tech Tips,正好提到这事儿,摘录如下:
The primitives are equal and the values of the boxed ints are equal. But this time the ints point to different objects. What you have discovered is that for small integral values, the objects are cached in a pool much like Strings. When i and j are 2, a single object is referenced from two different locations. When i and j are 2000, two separate objects are referenced. Autoboxing is guaranteed to return the same object for integral values in the range [-128, 127], but an implementation may, at its discretion, cache values outside of that range. It would be bad style to rely on this caching in your code.相关链接:http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
http://java.sun.com/developer/JDCTechTips/2005/tt0405.html