What is the constant pool in Java?

4

I'm reading the Java Virtual Machine Specification to dig a little deeper and I did not quite understand what the constant pool table is. For example, when speaking of the run-time constant pool the specification quotes this table:

  

A run-time constant pool is a per-class or per-interface run-time   representation of the constant_pool table in a class file (§4.4). It   contains several kinds of constants, ranging from numeric literals   known at compile-time to method and field references that must be   resolved at run-time. The run-time constant pool serves a function   similar to that of a conventional table   language, although it contains a wider range of data than a typical   symbol table.

Translation:

  

The run-time constant pool is a run-time representation per class or interface of the constant_pool table in a .class file (§4.4). It contains several types of constants, ranging from known numeric literals at compile time to method and field references that must be resolved at run time. The run-time constant pool serves a symbol-table-like function for a conventional programming language, although it contains a wider range of data than a typical symbol table.

What types of constants exist in this table and what is its utility? I would also like to know the difference between constant pool and run-time constant pool.

    
asked by anonymous 29.07.2016 / 17:10

1 answer

6

When the .java file is compiled, a .class file is generated. This .class file has the Java Byte Codes that are the java code transformed into generic machine instructions. These byte codes use data such as variable names, literal values, class references, method references, and so on. To save the amount of bytes required to represent this data, the byte codes use the constant pool. The constant pool has all this data and the byte code has a reference to it.

This link talks about byte codes: link

Each .class file has a constant pool. In addition, when .java is compiled, links are created with all classes that it relates through symbolic references. It is called a symbolic reference because it does not represent the actual memory address. Symbolic references are also in the constant pool.

You can see the contents generated in .class by using the following command in cmd:

javap.exe -verbose NomeAbsolutoDoArquivo.class > NomeAbsolutoDeUmArquivoTxt.txt

The file will be written in the file NameObjectTypeTxt.txt the contents of .class in human language. In this file you can see the constant pool generated. To execute this command you must be inside the% JAVA_HOME% \ bin directory.

When the JVM is instantiated and the class is loaded, a run-time constant pool is created that is based on the constant pool. When a symbolic reference needs to be used, it will be translated into the actual address.

This link talks about constant pool x run-time constant pool and the symbolic reference: link

The JVM, as its name implies, is a virtual machine. This machine will be responsible for, among other things, doing the memory management used by the running java program. The JVM is instantiated when the program is started.

An explanation of how the JVM works can be found in this article: link

The JVM has some memory areas. Two well-known areas are HEAP and STACK. All objects are in HEAP. STACK works as follows: Each time a method is invoked, a frame containing local variables, return value, operand stack, and the reference to the run-time constant pool of the class that owns the method will be created. This frame will be added at the top of the stack. When the method finishes executing and returning, the frame will be removed from the stack. The stack follows the LIFO (Last In First Out) data structure.

A very good explanation of JVM memory management can be found here: link

In addition to HEAP, there is an area called NON-HEAP. In this area is located the run-time constant pool. The classes that will be used by the program will also be loaded in that area. It is in NON-HEAP that PermGen is explained in the article by Luiz Ricardo quoted above.

The following image shows the three areas of memory discussed, shows that the run-time constant pool is in NON-HEAP and that it is referenced by the frame that is in STACK:

Thislinkspeaksindetailaboutjvm: link

Conclusion:

  

What is a constant pool for?

Save information that will be used by byte code.

  

What types of data are in the constant pool?

Literal values, class references, method references, attribute references, constants.

  

What is the advantage of the constant pool?

Save the amount of bytes used in the byte code.

  

What is the difference between constant pool and run-time constant pool?

The constant pool is in the .class. The run-time constant pool is in NON-HEAP, it exists when the class is loaded in jvm. It is based on the constant pool.

    
04.08.2016 / 22:39