A very interesting phenomenon, the result of the output of the following two results is false true, which is why?
Look at the source of the integer can be seen, when the new Integer (12), nothing special, is constructed by constructing an Integer object, and assigns 12 to the object's variable value. So the a!=b is normal. And the above C==d is true is very strange, with javap-c Test.class view compiled results found: Integer c = 12; This operation is optimized in the JVM to be integer.valueof (12);
Then the valueof (int i) of the integer is reached, and this method:
Found a very interesting thing, in the integer there is a integercache this inner class:
It adds 256 objects (from-128 to 127) at initialization time, and this maximum value of 127 can be configured by Autoboxcachemax, just by default 127], which is the cache for the integer, which means it takes an integer Cache[]; this.value =-128 ~ 127 was created in advance into the cache. Return to the above analysis of the integer c = 12; with the integer d = 12;c==d found that the original C and D are essentially the same object in the cache, and the natural return is true. Understand this is not to think a lot of problems solved!
Joooooe
Links: http://www.jianshu.com/p/7c23cab491b5
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Interesting integercache.