JDK source code learning-cache of integer basic data

Source: Internet
Author: User

1. integer, long, float, and double Basic Data Types

Problem Source: I accidentally saw integer today. the valueof () method, and the view information shows 【. valueof () is a static method. It is called several times to create an integet object at the same time and is finally called to the same integer instance.] I am not familiar with jdk api implementation, so I don't know how to do this. Therefore, I will write an example for verification:

Integer a = Integer.valueof(10);Integer b = Integer.valueof(10);Integer c = null;c=10;a=11;

Finally, I set a breakpoint at C = 10. I checked the ID number of each object and found that instance A, instance B, and instance c are all the same instance ID number, therefore, A, B, and C are the same object.
The results exactly match the information described above. When I reset the value of a = 11;, the instance id of object A is changed to a new ID, which is equivalent to re-instance an integer object.
I did not understand this phenomenon. According to the normal thinking, A = 11; this code should only reset the value attribute in the integer instance, why is a new instance
What about integer objects? I am not clear, so I have to check the JDK 1.5 API source code.
Open the integer class, and the methods in it open my eyes. I found that the gap between myself and the Masters is too big. So I will continue to study hard and follow them until I go beyond (low-key ...)
Based on the above [problem source], I have several questions that need to be answered through the JDK source code.
1. Why does the same instance get when integer. valueof () is called by calling A, B, and C variables?
2. Why does C = 10 automatically become an instance equivalent to a and B?
3. Why is a new ingeter object generated after executing a = 11?
Reading things with questions will always be the most effective.
When I open the integer class, I can see a piece of code.

public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

4. The meaning of the Code is not very clear, and I don't know what the type will do. The problem is that I didn't find the class. getprimitiveclass () method in the jdk1.5 API documentation.
Put down this question without affecting my answers to the above three questions.

Answer:
Why does the call of integer. valueof () Get the same instance and you have to find the valueof () method. The method code is as follows:

Public static integer valueof (int I) {final int offset = 128; if (I >=- 128 & I <= 127) {// must cache can only cache-128 ~ If 127 of the data exceeds this range, the ingeter object is newly instantiated. Return integercache. cache [I + offset];} return New INTEGER (I );}

Integercache is a member internal class of the integer class. The Code is as follows:

Public final class integer extends number implements comparable <integer> {... ... Private Static class integercache {private integercache () {} static final integer cache [] = new integer [-(-128) + 127 + 1]; // set an array for storing integers statically, that is, a cache // initialize the cache, and you will find that only-128 ~ is cached in the cache ~ 127 integer instances in this range. Static {for (INT I = 0; I <cache. length; I ++) cache [I] = new INTEGER (I-128 );}}... ... }

It is inferred from the source code logic that only the values (-128 ~ 127) for data in this range class, the instantiated objects in the cache will be used only by calling integer. valueof (input value.

Data beyond this range is newly instantiated objects. Next we will verify this conclusion:

Integer e = Integer.valueof(128);Integer f = Integer.valueof(128);

Verification found that E and F have different instance numbers. The description is not in (-128 ~ 127) data within the range of calling integer. valueof () is to generate a new instance.

The function of automatic packing and unpacking is in fact the compiler to help you. During the compilation period, the compiler determines whether to perform packing or unpacking Based on the syntax you have compiled. When auto-packing is performed for values ranging from-128 to 127, they are packed as integer objects and will be reused in memory, therefore, when using = for comparison in example 4.6, I1 and I2 actually refer to the same object. If the value range from-128 to 127 is exceeded, the boxed integer object is not reused, which is equivalent to creating an integer object each time it is packed, therefore, for example 4.7, when using = for comparison, I1 and I2 refer to different objects. Therefore, do not rely too much on automatic packing and unpacking. You must still know the basic data type and
Object difference.

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.