19, Java memory allocation constant pool detailed

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."

Ext.: http://hi.baidu.com/rickmeteor/blog/item/f0be11dff578ba1662279848.html

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.

1, constants in the pool objects and objects in the heap

public class test{

Integer i1=new integer (1);
Integer i2=new integer (1);
I1,i2 in different memory spaces in the heap, respectively

System.out.println (I1==I2);//Output False


Integer i3=1;
Integer i4=1;
I3,i4 points to the same memory space in a constant pool

System.out.println (I3==I4);//Output True

Obviously, I1,i3 is in a different memory space.

System.out.println (I1==I3);//Output False

}

2, 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:

public class test{

public static void Main (string[] args) {

5 Kinds of Byte,short,integer,long,character objects of plastic packaging class,

You can use a constant pool when the value is less than 127

Integer i1=127;

Integer i2=127;

SYSTEM.OUT.PRINTLN (I1==I2)//Output True

When the value is greater than 127, the object is not taken from the constant pool

Integer i3=128;

Integer i4=128;

SYSTEM.OUT.PRINTLN (I3==I4)//Output False

The Boolean class also implements the constant pool technique

Boolean bool1=true;

Boolean bool2=true;

System.out.println (bool1==bool2);//Output True

Floating-point type wrapper class does not implement constant pool technology

Double d1=1.0;

Double d2=1.0;

SYSTEM.OUT.PRINTLN (D1==D2)//Output False

}

}

3,string also implements the constant pool technology

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:

public class test{

public static void Main (string[] args) {

S1,s2 in different spaces in the heap

String S1=new string ("Hello");

String S2=new string ("Hello");

SYSTEM.OUT.PRINTLN (S1==S2)//Output False

S3,S4 in the same space in the pool

String s3= "Hello";

String s4= "Hello";

System.out.println (S3==S4);//Output True

}

}

At last:

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

Additions to the Integer object: http://hi.baidu.com/fandywang_jlu/blog/item/c5590b4eae053cc3d1c86a13.html

The integer package:

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);

}
When you give an integer object an int value, in fact it calls the ValueOf method, and then you assign this value is very special, is 128, then does not have the cache method, the equivalent of the new two of the object. So the two lines of code that define a and B in the question are similar to the following:

Integer a = new integer (128);

Integer b = new integer (128);
I'll ask you this time, what is the result of the output? You know it's false. If you change this number to 127, then execute:

Integer a = 127;

Integer B = 127;

System.out.println (A = = B);
The result is: true

From the above, it is best to use equals when comparing objects, which is easy to control according to our own purposes.

--------------------------------------------------Supplement--------------------------------------------------------------------- --

Let's take a look at the contents of the Integercache class:

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);

}

}

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.

19, Java memory allocation constant pool detailed

Related Article

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.