Interesting integercache.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.