The key word here is integer caching. Integer values ββunder 128 * are cached, and ++ returns the result of interning valueOf .
There are two fun experiments you can perform to understand how Integers caching works:
- Replace
new Integer(2) with Integer.valueOf(2) and immediately notice that x==y - Replace
2 with 200 and note that x++ and y++ return different objects
<h / "> * It is possible to control this: java.lang.Integer.IntegerCache.high=high_val
source share