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.