Java: Use of wrapper-type buffering mechanisms (reprint)

Source: Internet
Author: User
Tags lua

Summary: Eight basic data types and their wrapper classes in Integer valueOf (int i), byte valueOf (Byte b), short valueOf (short s), Long valueOf (long L), Character Valu EOf (char c) uses a buffering mechanism, and the buffer range is -128~127 However, there is no buffer mechanism available for wrapper class Float,double,boolean

In order to understand the use of buffering mechanism, we first start with the mutual transfer of integer and int:

JDK1.5 adds an entirely new approach to the integer:
Java Code Collection Code

public static Integer valueOf(int i)

The following code can be compiled and run in a JDK1.5 environment.

int i = 0;  Integer wrapperi = Integer.valueOf(i);  

The difference between this method and the new Integer (i) is:
Method one calls the class method to return an Integer instance that represents the specified int value.
Method two produces a new integer object.

The new valueof method is clearly explained in the JDK API documentation:
If you do not need a new integer instance, you should generally prefer to use this method instead of constructing method Integer (int), because it is possible for the method to significantly improve spatial and temporal performance by caching the frequently requested values.

But the explanation is a bit obscure. Why is it possible for this method to significantly improve performance by caching frequently requested values?

View the valueof method by using the Anti-compilation tool.

Public Static Integer  Valueof ( int  i) final int offset = 128; if (i >=-128 && i <=  127) {//must cache return integercache.cache[i + offset];} Span class= "Hljs-keyword" >return new Integer (i);}        

You can see that for integers ranging from 128 to 127, the ValueOf method does special processing.
Use Integercache.cache[i + offset] this method.
From the name, we can guess that this is some kind of caching mechanism.

Further tracking Integercache This class, such code is as follows

PrivateStaticClass Integercacheprivate < Span class= "Hljs-title" >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);}}     

This is the real optimization method of the ValueOf method, when -128=

Integer i=100;  Integer j=100;  //print true  System.out.println(i==j);  

I=integercache.cache[i +] = integercache.cache[228],
Same J = integercache.cache[j + +] = intgercache.cache[228]
Therefore, the integer reference I stores the address of the No. 228 element of the cache array. Similarly j is also the address of the No. 228 element of the same cache array (because the cache is an integer static array with only one).
I==j compares the reference address, so it returns true.

Integer i=200;  Integer j=200;  //print false  System.out.println(i==j);   

The I=new Integer (200) at this time; The same j=new Integer (200).
Both of them opened up an integer object in the heap.
The object address of the heap stored in I and J is completely different. I==j naturally returns false.

What is the role of introducing a caching mechanism?

Then the above example, if we are programming, we need an integer object with a value of 100 (100 in the range 128 to 127). If created only through new, you need to create a large number of integer objects of the same value in the heap.
This is quite uneconomical, and Integercache.cache is good at caching.
When we need the integer i = 100, the address of the [100+128] element directly from the cache is assigned to the reference I, again when the integer j = 100 is required, or the address is directly assigned to J. is not the cost of constantly creating objects in the heap (space, time consumption is very large). This is where the valueof approach really improves performance.
As the JDK API documentation describes the valueof (int i) method, it is possible that the method can significantly improve spatial and temporal performance by caching the frequently requested values.

Conclusion
The optimization of valueOf (int i) is only for integers ranging from 128 to 127. While the overloaded valueOf (string s), ValueOf (string s, int radix) is not using the buffering mechanism

In summary: (by querying the underlying code to know:)
The eight basic data types and their wrapper classes are Integer valueOf (int i), byte valueOf (Byte b), short valueOf (short s), Long valueOf (long L), Character valueOf (char c) has a buffering mechanism, and the buffer range is -128~127
However, the wrapper class Float,double,boolean does not provide the appropriate buffering mechanism

Original address: http://blog.csdn.net/w124374860/article/details/50899507

Java: Use of wrapper-type buffering mechanisms (reprint)

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.