JDK auto boxing/unboxing Optimization

Source: Internet
Author: User
Document directory
  • [Introduction]
  • [Decryption]
  • Note]
[Introduction]

First, let's look at a piece of code:

Package test;/***** @ author whwang * 2011-12-3 11:44:04 */public class test {public static void main (string [] ARGs) {INTEGER I1 = 100; integer I2 = 100; system. err. println (I1 = I2); integer i12 = 200; integer i22 = 200; system. err. println (i12 = i22 );}}

It seems that the two parts are "identical", but one output is true and the other is false. Why?

[Decryption]

First, you need to know the principle of automatic packaging and unpackaging of JDK. packaging is to call the valueof () method of the packaging class. The above integer I1 = 100 is equivalent to integer I1 = integer. valueof (100): The xxxvalue () corresponding to the unpackage call. For example, integer calls the intvalue () method to unpackage. Next let's take a look

Look at the integer. valueof () method. The source code is as follows:

public static Integer valueOf(int i) {    final int offset = 128;    if (i >= -128 && i <= 127) { // must cache        return IntegerCache.cache[i + offset];    }    return new Integer(i);}

In this method, determine whether the value is in the [-128,127] interval. If yes, return integercache. cache [I + offset]. Integercache is a private internal class in integer. Its code is as follows:

private static class IntegerCache {    private IntegerCache() {    }    static final Integer cache[] = new Integer[-(-128) + 127 + 1];    static {        for (int i = 0; i < cache.length; i++)            cache[i] = new Integer(i - 128);    }}

In integercache, you can create packaging objects with static values equal to [-128,127] and cache them. Let's take a look at the Code mentioned at the beginning of the article, system. err. println (I1 = I2) since the I1 and I2 values are 100, they are between [-128,127]. Therefore, I1 and I2 return cache objects, and the two objects are identical, output True. While i12 and i22 are not in the [-128,127] range. In the valueof () method, the return New INTEGER (I) is not an integer in this range ), therefore, the i12 and i22 addresses are different, and the output is false. Similarly, byte, short, and long are all similar to integer, and all have objects with cache values between [-128,127.

The benefits of doing so are obvious. If we use a large number of integer objects in the range [-128,127] in our program, we do not need to use new to instantiate the object, instead, they are taken directly from the cache to save time/space.

Note]

We can see that in the byte, short, integer, and long packaging classes, there are three overloaded valueof () methods. The following example uses Integer as an example:

Integer. valueof (100); integer. valueof ("100"); // equivalent to integer. valueof ("100", 10); integer. valueof ("100", 10 );

In the three methods, only the first integer. valueof (INT) method uses the cache, and the other two methods both return directly to the new object. Byte, short, and long are similar to integer.

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.