Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

An Introduction to the Constant Pool in the JVM

$
0
0

1. Introduction

When we compile a .java file, we get a separate class file with a .class extension. The .class file consists of several sections and a constant pool is one of them.

In this quick tutorial, we're going to explore the details of a constant pool. Also, we'll see what are types it supports and how it formats the information.

2. Constant Pool in Java

Simply put, a constant pool contains the constants that are needed to run the code of a specific class. Basically, it's a runtime data structure similar to the symbol table. It is a per-class or per-interface runtime representation in a Java class file.

The content of the constant pool consists of symbolic references generated by the compiler. These references are names of variables, methods, interfaces, and classes referenced from the code. The JVM uses them to link the code with other classes it depends on.

Let's understand the structure of a constant pool using a simple Java class:

public class ConstantPool {
    
    public void sayHello() {
        System.out.println("Hello World");
    }
}

To view the constant pool's content, we need to first compile the file and then run the command:

javap -v name.class

The above command will yield:

   #1 = Methodref          #6.#14         // java/lang/Object."<init>":()V
   #2 = Fieldref           #15.#16        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #17            // Hello World
   #4 = Methodref          #18.#19        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #20            // com/baeldung/jvm/ConstantPool
   #6 = Class              #21            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               sayHello
  #12 = Utf8               SourceFile
  #13 = Utf8               ConstantPool.java
  #14 = NameAndType        #7:#8          // "<init>":()V
  #15 = Class              #22            // java/lang/System
  #16 = NameAndType        #23:#24        // out:Ljava/io/PrintStream;
  #17 = Utf8               Hello World
  #18 = Class              #25            // java/io/PrintStream
  #19 = NameAndType        #26:#27        // println:(Ljava/lang/String;)V
  #20 = Utf8               com/baeldung/jvm/ConstantPool
  #21 = Utf8               java/lang/Object
  #22 = Utf8               java/lang/System
  #23 = Utf8               out
  #24 = Utf8               Ljava/io/PrintStream;
  #25 = Utf8               java/io/PrintStream
  #26 = Utf8               println
  #27 = Utf8               (Ljava/lang/String;)V

#n indicates the references to the constant pool. #17 is a symbolic reference to the “Hello World” String, #18 is System.out, and #19 is a println. Similarly, #8 highlights that the return type of method is void and #20 is a fully qualified class name.

It is important to note that the constant pool table starts from index 1. The index value 0 is regarded as an invalid index.

2.1. Types

The constant pool supports several types:

  • Integer, Float: with 32-bit constants
  • Double, Long: with 64-bit constants
  • String: a 16-bit string constant that points at another entry in the pool which contains the actual bytes
  • Class: contains the fully qualified class name
  • Utf8: a stream of bytes
  • NameAndType: a colon-separated pair of values, first entry represents the name while the second entry indicates the type
  • Fieldref, Methodref, InterfaceMethodref: a dot-separated pair of values, first value points at Class entry whereas the second value points as NameAndType entry

What about the other types like boolean, short, and byte? These types are represented as Integer constant in the pool.

2.2. Format

Each entry in the table obeys a general format:

cp_info {
    u1 tag;
    u1 info[];
}

The initial 1-byte tag indicates the kind of constant. Once a JVM grabs and intercepts the tags, it knows what follows the tag. Usually, the tag is followed by two or more bytes to carry the information about that constant.

Let's look at some of the types and their tag indexes:

  • Utf8: 1
  • Integer: 3
  • Float: 4
  • Long: 5
  • Double: 6
  • Class reference: 7
  • String reference: 8

The constant pool of any class or interface is created only when the JVM has completed its loading.

3. Conclusion

In this quick article, we learned about the constant pool in JVM. We've seen that it contains symbolic references that are used to locate the actual objects. Also, we look at how the pool format the information about constants and their types.

As always, the code snippet can be found over on Github.

The post An Introduction to the Constant Pool in the JVM first appeared on Baeldung.
        

Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>