The Java foundation---The integer caching mechanism---the integer caching mechanism

Source: Internet
Author: User

Automatic boxing is triggered when automatic boxing is used, that is, when the basic data type is passed to the object class. At this point, the Java Virtual Opportunity creates a series of integers and caches them in an array for immediate use, which is the caching strategy.  === Automatic boxing mechanism The Java compiler automatically converts the original type to an encapsulated class called Auto-Boxing (autoboxing), which is equivalent to calling the ValueOf method integer A = 10; This is autoboxinginteger B = integer.valueof (10); Under the Hood  === integer cache policy this integer cache policy is only useful when auto-boxing (autoboxing), and an integer object created with the constructor cannot be cached. The above rules apply to integer intervals-128 to +127. Because Integer.valueof is where the cache policy is executed  ===integer.valueof (int i)    public static Integer valueof (int i) {        if (i >= integercache.low && i &amp ; lt;= Integercache.high)            return Integercache.cache[i + (-integercache.low)];       return new Integer (i);    } ===integercachejavadoc detailed description of this class is used to implement cache support, and supports automatic boxing between 128 and 127. The maximum value of 127 can be modified by-xx:autoboxcachemax=size the JVM's startup parameters. The cache is implemented through a for loop. Create as many integers as you can from small to large and store them in an array of integers named cache. This cache willInitialized when the Integer class is used for the first time. Later, you can use the instance objects contained in the cache instead of creating a new instance (in case of automatic boxing).      private Static Class Integercache {         static final int low = -128;        static final int high;   & Nbsp;    static Final Integer cache[];          static {            //High value is configured by Property            int h = 127;             string Integercachehighpropvalue =                 sun.misc.vm.getsavedproperty (" Java.lang.Integer.IntegerCache.high ");             if (integercachehighpropvalue! = null) {                try {                      int i = parseint (integercachehighpropvalue);                     i = Math.max (i, 127);                     //Maximum Array size is integer.max_value                     h = Math.min (i, Integer.max_value-(-low)-1);                 } catch (NumberFormatException nfe) {                     // If the PROperty cannot be parsed to an int, ignore it.                 }            }             high = h;              cache = new integer[(high-low) + 1];      & Nbsp;     int j = low;             for (int k = 0; k < cache.length; k++)                  CACHE[K] = new Integer (j + +);              //range [ -128, 127] must be interned (JLS7 5.1.7)       & Nbsp;     assert Integercache.high >= 127;  &NBsp;     }         p  === Other buffering behavior other cached objects This caching behavior applies not only to integer objects. We have a similar caching mechanism for classes of all integer types. There are bytecache used to cache a Byte object that has shortcache for caching a short object that has longcache for caching a Long object with Charactercache for caching Character objects Byte,short,lon G has a fixed range: 128 to 127. For Character, the range is 0 to 127. Except the Integer can be changed by the parameter range, the others are not.  

Java Foundation---The integer caching mechanism---the integer caching mechanism

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.