What is the purpose of the Java Constant Pool?

I am currently trying to delve into the specification of a Java virtual machine. I read Inside the JVM book on the Internet , and there is one confusing abstraction that I cannot understand: Constant Pool. here is an excerpt from the book:

For each load type, the Java virtual machine must store a persistent pool. A constant pool is an ordered set of constants used by a type, including literals (string, integer, and floating point constants) and symbolic links to types, fields, and methods. Entries in the constant pool reference an index, like array elements. Because it contains symbolic links to all types, fields, and methods used by the type, the constant pool plays a central role in the dynamic linking of Java programs

I have a few questions about the above and CP in general:

  • Is CP available in the .class file for each type?
  • What does the "symbolic link" author mean?
  • What is the goal of Constant Pool, in plain English?
+47
java vm-implementation class-constants
Apr 18 2018-12-18T00:
source share
4 answers

I think understanding how a frame will be built using a chart will help.

enter image description here

The operands (operating instructions) are in the frame, and there is dynamic linking. This is a shorthand way, so to speak, to use a constant pool to track the class and its members.

Each frame contains a link to a pool of runtime constants. The link points to a constant pool for the method class that runs for this frame. This link helps maintain dynamic linking.

C / C ++ code is usually compiled into an object file, and then several object files are linked together with the product of a usable artifact, such as an executable or dll. During the linking phase, symbolic links in each object file are replaced with the actual memory address relative to the final executable file. In Java, this binding phase is performed dynamically at run time.

When the Java file is compiled, all references to variables and methods are stored in the class constant pool as a symbolic link. A symbolic link is a logical link, not a link that actually points to physical memory.

Here is a link to the James Blooms JVM Internals for more details.

+50
Dec 03 '13 at 17:24
source share

The constant pool is part of the .class file (and its representation in memory), which contains the constants necessary to run the code for this class.

These constants include literals specified by the programmer and symbolic links generated by the compiler. Symbolic links are basically the names of the classes, methods, and fields that the code refers to. These links are used by the JVM to associate your code with other classes on which it depends.

For example, the following code

 System.out.println("Hello, world!"); 

outputs the following bytecode ( javap output)

 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, world! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 

#n Here are links to the persistent pool. #2 is a symbolic link to the System.out field, #3 is the string Hello, world! , and #4 is a symbolic link to the PrintStream.println(String) method.

As you can see, symbolic links are not just names - for example, a symbolic link to a method also contains information about its parameters ( Ljava/lang/String; ) and the return type ( V means void ).

You can check the persistent pool of a class by running javap -verbose for that class.

+63
Apr 18 2018-12-18T00:
source share

What is a permanent pool target in plain English?

CP is a memory area. To reduce redundancy, very unique constant values ​​are stored:

 System.err.println("Hello"); System.out.println("Hello"); 

There is only one String "Hello" object in CP, and the compiler replaces the same link on both lines. The .class file contains only one line of Hello. (Same for other types).

Is the CP in the .Class file for each type?

Yes, see here: http://en.wikipedia.org/wiki/Java_class_file

+6
Apr 18 2018-12-18T00:
source share

Let's give an Example First to understand what a constant pool of strings means.

  public class StringConstantPool { public static void main(String[] args) { String s = "prasad"; String s2 = "prasad"; System.out.println(s.equals(s2)); System.out.println(s == s2); } } 

the conclusion will be

 true true 

what happens here step by step

1- The class is loaded when the JVM is called.

2- The JVM will search for all string literals in the program.

3- Firstly, it finds the variable s , which refers to the literal "prasad" and will be created in memory

4- The reference to the literal "prasad" will be placed in the memory of the string constant string pool.

5- Then he finds another variable s2 , which refers to the same string literal "prasad".

Now that the JVM has already found the string literal "prasad", both s and s2 refer to the same object, i.e. prasadam.

enter image description here

I hope it will be useful

more details http://www.journaldev.com/797/what-is-java-string-pool

+1
Mar 01 '15 at 13:01
source share



All Articles