Home  >  Article  >  Java  >  What is constant pool in Java? Introduction to Java constant pool

What is constant pool in Java? Introduction to Java constant pool

不言
不言Original
2018-09-20 14:40:448417browse

This article brings you what is the constant pool in Java? The introduction of Java constant pool has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Constant pool in Java

In the Java virtual machine jvm, the memory distribution is: virtual machine heap, program counter, local method stack, virtual machine stack, method area.

What is constant pool in Java? Introduction to Java constant pool

The program counter is the pipeline of the jvm execution program and is used to store some instructions. The local method stack is the stack used by the jvm operating system method, and the virtual machine stack is The stack used to execute program code has class variables, class information, method information, and constant pools (symbol references, in the form of tables) in the method area. The heap is the heap used by the virtual machine to execute program code.

constant? It is a quantity that cannot be changed once the value is given. Member variables modified with final are constants.

What is a class file constant pool?

We know that in the class file, there is class version information, field information, methods, interfaces and other information, and there is also a constant pool. This is the class file constant pool.

What is the class file constant pool mainly used to store?

stores various literals and symbol references generated by compilation. In computer science, literals are representations used to express fixed values ​​in source code; symbolic references are a set of symbols used to describe the referenced target. They can be any form of literals, as long as they can be used unambiguously. Just locate the target.

The constant pool exists in the form of a table (the table is used to store string values, not symbol references). It can actually be divided into two types, one is the static constant pool, and the other is the runtime constant pool. There are 11 constant tables in the constant pool. Each constant in the constant pool represents a table.

Constant table

Constant table type Flag value Description
CONSTANT_Utf8 1 UTF-8 encoded Unicode string
CONSTANT_Integer 3 Literal value of type int
CONSTANT_Float 4 Literal value of type float
CONSTANT_Long 5 Literal value of type long
CONSTANT_Double 6 Literal value of double type
CONSTANT_Class 7 Symbolic reference to a class or interface
CONSTANT_String 8 A reference to a literal value of type String
CONSTANT_Fieldref 9 Yes The symbol for a field
CONSTANT_Methodref 10 Apply the symbol for a method in a class
CONSTANT_InterfaceMethodref 11 A symbolic reference to a method in an interface
CONSTANT_NameAndType 12 To an Partial symbol reference of field or method

Constant pool

Integer integer1 = 127;
Integer integer2 = 127;
System.out.println(integer1 == integer2);
// true
Integer integer1 = 128;
Integer integer2 = 128;
System.out.println(integer1 == integer2);
// false

In Java, the symbol "==" is used to compare addresses. The symbol "equals" defaults to the same symbol as "==", which is used to compare addresses.

String string1 = "dashu";
String string2 = "dashu";
System.out.println(string1==string2);
// true
String string1 = "dashu";
String string3 = new String("dashu");
System.out.println(string1 == string3);
// false

String str = new String("dashu"); How many objects are created?
The answer is: 2 or 1.

In new String("dashu");, if the "dashu" literal value already appears in the constant pool, then only one object will be created, if not, two objects will be created.

Principle: When the literal "dashu" appears, the system will check whether the same string exists in the string constant pool. If so, it will not create a new object, otherwise it will use the literal The value "dashu" creates a String object. And new String("dashu"), with the keyword new, means that it will definitely create a new object, and then call the constructor that receives the String parameter for initialization.

If changed to string1 == string3.intern(), the result is true, because the address of the literal value in the constant pool is returned.

Stack: thread stack and native method stack

// 源码
public class Object{
 private static native void registerNatives();
 static{
  registerNatives();
 }
}
// 源码
public boolean equals(Object obj){
 return (this == obj);
}
// 源码
public String toString(){
 return getClass().getName() + "@" + Integer.toHexString(hasCode());
}
// 源码
protected native Object clone() throws CloneNotSupportedException;

has the native modifier to call c through JNI Language or c is executed.

All classes are subclasses of Object.

万物皆对象
// 源码注解
Class {@code Object} is the root of the  class hierarchy.
Every class has {@code Object} as a superclass.
All objects, including arrays, implements the methods of this class.
@ see java.lang Class
@ since JDK1.0

Constant pool:
Store all constants in the Class file
It is said in Java that the constant pool can be divided into two forms, the static constant pool and the runtime constant pool.

The static constant pool is the constant pool in the class file, which contains string literals, class information, method information, etc. It occupies a large part of the space of the class file. The constant pool mainly stores literals. and symbol references.

The runtime constant pool is the operation of the Java virtual machine after completing the class loading. It loads the constant pool in the class file into the memory and ensures that it is in the method area. The constant pool in our mouth is in the method area. Running constant pool, the running constant pool is dynamic, and new constants can be generated and put into the pool during running, which is the code written above. Constants do not have to be generated during compilation, new outputs can also be generated during runtime and put into the pool.

The following analysis:

When the Java virtual machine jvm executes a certain class, it must go through the class from loading into memory to unloading.

The whole process is loading, verification, preparation, parsing, initialization, use, and uninstallation.

Load,

Verify whether the version of the class file is compatible with the current Java virtual machine version, and then the class file must meet the specifications of the virtual machine.

Preparation, what do you need to prepare?
It is necessary to initialize class members to their initial values, except for class variables modified by final. Final variables are directly initialized to variable values, while class members are different.

Analysis, what is analysis?
is to parse the symbol reference into a direct reference, which is our variable xxx. This representation becomes a direct reference. What is a direct reference? It is the memory address, such as our common xxx0203r0e.

Initialization, use static modified variables or static static code blocks to form a constructor in order to initialize the variables.

Use,

Uninstall

When the class is loaded into the memory, the jvm will store the contents of the class constant pool into the runtime constant pool, so the runtime constant pool Each class has one.

The class constant pool stores references to literals and symbols. It is the symbol reference value of the object. After parsing, the symbol reference is parsed into a direct reference. During the compilation phase, the symbol reference of the constant is stored. After parsing That's a direct quote. Then ensure that each jvm has only one copy in the global constant pool, which stores the direct reference value of the string constant.

If changed to `string1 == string3.intern()`, the result will be true, because the address of the literal value in the constant pool is returned.

The intern() method of the String class will search the constant pool to see if there is a string that is equal to equal().

String string1 = "dashu";
String string3 =  new String("dashu");
System.out.println(string1==string3.intern());

If there is no "dashu" literal in the constant pool, then put the "dashu" value of this literal into the constant table first. , and then returns the address of the constant table.

Advantages of the constant pool

The constant pool can avoid the frequent creation and destruction of objects, which leads to a decrease in system performance, and also realizes the sharing of objects, which can save memory space and Running time.

The above is the detailed content of What is constant pool in Java? Introduction to Java constant pool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn