Java constant pool concept detailed and purpose

Source: Internet
Author: User

In the class file, "constant pool" is the most complex and most noteworthy content.

Java is a dynamically connected language, the role of a constant pool is very important, in addition to the constant pool contains the various basic types defined in the code (such as int, long, and so on) and the object type (such as string and array) of the constant value also contains some of the textual form of symbolic references, such as:

The fully qualified name of the class and interface;

The name and descriptor of the field;

Methods and names and descriptors.

In C, if a program calls a function in another library, the position of the function in the library (that is, the offset from the beginning of the library file) is written in the program at the time of the connection, and the function is called directly to the address at runtime;

In the Java language, this is not the case, and everything is dynamic. At compile time, if a call to another class method or a reference to another class field is found, the record into the class file can only be a symbolic reference in text form, and during the connection, the virtual machine will find the corresponding method or field according to the text information.

So, unlike the so-called "constants" in the Java language, the "constant" content in a class file is not rich, and these constants are concentrated in one area of the class, followed by one, which is called a "constant pool."

The constant pool technique in Java, which occurs when an object is needed, can be taken out of the pool (if one is not created in the pool), and it will save a lot of time when you need to repeat the creation of equal variables. A constant pool is actually a memory space, unlike the heap space where objects created with the New keyword are located. This article only discusses Java constant pool technology from the Java user's point of view, and does not involve the principle and implementation method of constant pool. Personally, if you are really focused on Java, you have to have a certain understanding of these details. But it is not necessary to know the principle and the concrete way to implement it.

Objects in a constant pool and objects in a heap

[Java]View Plaincopy
  1. Public class test{
  2. Integer i1=new Integer (1);
  3. Integer i2=new Integer (1);
  4. //i1,i2 in different memory spaces in the heap, respectively   
  5. System.out.println (I1==I2); //Output False   
  6. Integer i3=1;
  7. Integer i4=1;
  8. //i3,i4 points to the same memory space in a constant pool   
  9. System.out.println (I3==I4); //Output True   
  10. //Obviously, the I1,I3 is in a different memory space   
  11. System.out.println (I1==I3); //Output False   
  12. }
8 basic types of wrapper classes and object pools

most of the basic types of wrapper classes in Java implement the constant pool technique, which is byte,short,integer,long,character,boolean, and the other two types of floating-point wrapper classes are not implemented. Also byte,short,integer,long,character these 5 types of integer wrapper classes can use object pooling only when the corresponding value is less than or equal to 127, which is why the object is not responsible for creating and managing objects greater than 127 of these classes. Here are some of the corresponding test codes:

[Java]View Plaincopy
  1. Public class test{
  2. Public static void main (string[] args) {
  3. ///5 kinds of Byte,short,integer,long,character objects of plastic packaging class,   
  4. //You can use a constant pool when the value is less than 127   
  5. Integer i1=127;
  6. Integer i2=127;
  7. System.out.println (I1==I2)//Output true
  8. //value is greater than 127, object is not taken from the constant pool   
  9. Integer i3=;
  10. Integer i4=;
  11. System.out.println (I3==I4)//Output false
  12. the//boolean class also implements the constant pool technology   
  13. Boolean bool1=true;
  14. Boolean bool2=true;
  15. System.out.println (bool1==bool2); //Output True   
  16. //floating-point type wrapper class does not implement constant pool technology   
  17. Double d1=1.0;
  18. Double d2=1.0;
  19. System.out.println (D1==D2)//Output false
  20. }
  21. }
String also implements the constant pool technique

The string class is also a much-used class in Java, and also implements the technique of constant pooling for the convenience of creating a String object, the test code is as follows:

[Java]View Plaincopy
  1. Public class test{
  2. Public static void main (string[] args) {
  3. //s1,s2 in different spaces in the heap   
  4. String s1=new string ("Hello");
  5. String s2=new string ("Hello");
  6. System.out.println (S1==S2)//Output false
  7. //S3,S4 in the same space in the pool   
  8. String s3="Hello";
  9. String s4="Hello";
  10. System.out.println (S3==S4); //Output True   
  11. }
  12. }
At last

Details determine success or failure, and writing code is more so.

Before JDK5.0, it is not allowed to directly assign the data of the basic data type directly to its corresponding wrapper class, such as: Integer i = 5;

However, this notation is supported in JDK5.0 because the compiler will automatically convert the above code into the following code: Integer i=integer.valueof (5);

This is the boxed java.  The JDK5.0 also offers automatic unpacking. Integer i = 5;  int j = i;

Integer encapsulation:

[Java]View Plaincopy
  1. Public static Integer valueOf (int i) {
  2. Final     int offset = N;
  3. if (i >=- && i <= 127) { //must cache   
  4. return    integercache.cache[i + offset];
  5. }
  6. return     New Integer (i);
  7. }
  8. Private static class Integercache {
  9. Private Integercache () {}
  10. Static  final Integer cache[] = new integer[-(-+) +     127 + 1];
  11. Static  {    
  12. for (int i = 0; i < cache.length; i++)
  13. Cache[i] = new Integer (i- N);
  14. }
  15. }
because cache[] is a static array in the Integercache class, that is, it only needs to be initialized once, that is, static{... } section, so if the integer object is initialized with a range of -128~127, there is no need to redefine the application space, which is the same object---in Integercache.cache, which can improve the efficiency to some extent.

Java constant pool concept detailed and purpose

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.