Java Integer constant Pool

Source: Internet
Author: User

Integer ClassThe Integer class wraps the value of a primitive type int in the object. An object of type Integer contains a field of type int. In addition, the class provides several methods for converting between int and String types, as well as some other constants and methods that are useful when working with int types. the difference between the integer i = 10 and the integer j = new Integer (10)
1 1. Publicstaticvoid Main (string[] args) {2 2.Integer i=10; 3 3.Integer J=newinteger (ten); 4 4.system.out.println (i==j); 5 5.}
View Code

Compiling the above code, we use the Anti-compilation tool to view:

 public  class   Test { 2  public  static  void   main (string[] args) { 3  Integer i = integer.valueof (10 4  integer j = new  Integer (10); 5  System.out.println (i = = j); // false  6   7  }
View Code

Thus, when we use Integer i=ten; When you create an integer, the underlying call is actually an integer. valueOf (ten);

Integer. valueOf ()in this way, the JDK says:
valueOf (int i)
int     Integer   instance. If you do not need a new   integer   instance, this method should usually be preferred instead of constructing method   integer (int) . Because the method has the potential to significantly improve spatial and temporal performance by caching frequently requested values.


parameter:

i-a int value.
Return:
Represents an Integer instance of I .
Start from the following versions:
1.5
This method always caches values between 128 and 127, and it can also cache values outside of this range.
 1  public  static  Integer valueOf (int   i) { 2  assert  Integercache.  High >= 127;  3  if  (I >= integercache. Low &         amp;& i <= Integercache. High)  4  return  Integercache.  Cache[i + (-integercache. Low)];  5  return  new   Integer (i);  6 } 
View Code

The Integercache here is actually an Integer a static inner class:

1 Private Static classIntegercache {2         Static Final intLow =-128;3         Static Final intHigh ;4         Static FinalInteger cache[];5         Static {6             //High value is configured by property7             intH = 127;8String Integercachehighpropvalue =9Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");Ten             if(Integercachehighpropvalue! =NULL) { One                 inti =parseint (integercachehighpropvalue); Ai = Math.max (i, 127); -                 //Maximum array size is Integer.max_value -h = math.min (i, Integer.max_value-(-low)-1); the             } -High =h; -Cache =Newinteger[(high-low) + 1]; -             intj =Low ; +              for(intk = 0; K < Cache.length; k++) -CACHE[K] =NewInteger (j + +); +         } A         PrivateIntegercache () {} at}
View Code

From the code above, we can clearly see that when we call valueof () in the range 128 to 127, we are actually using the values in the cache (Integercache).

Integercache is a class that helps us to cache integers. The size of the cached value is controlled by-xx:autoboxcachemax= <size>, or can be obtained through System Properties:-djava.lang.integer.integercache.high=<size>
in other words, when we create an integer class in the following ways:
1 Integer int1=10; 2 Integer int2=integer.valueof (10);
View Code

Will get the same instance on the cache pool.

So if the value is between 128 and 127, you will get the same reference using valueof (), which will return the new Integer (int). Because it is the same reference, your = = operation is useful in the range of-128 to 127.
Java caches an integer class in the range 128 to 127. So when you assign a value to a package class of this scope, the boxing operation calls the Integer.valueof () function, which in turn gives the encapsulated class A reference to the cache pool instance.
on the other hand, integer.valueof () creates a new Integer class if the encapsulated class is assigned a value that exceeds this range. Therefore, it returns false when compared to an integer class that exceeds this range. Similarly, short, Byte, and long also cache values from 128 to 127.
from for Notes (Wiz)



Java Integer constant Pool

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.