Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding javap's output for the Constant Pool

Tags:

When running javap on a very simple HelloWorld application I have some confusion on the output around the constant pool.

Test Code

public class TestClass {     public static void main(String[] args) {         System.out.println("hello world");     } } 

Javap -c -verbose output (snipped)

// Header + consts 1..22 snipped const #22 = String      #23;    //  hello world const #23 = Asciz       hello world;  public static void main(java.lang.String[]);   Signature: ([Ljava/lang/String;)V   Code:    Stack=2, Locals=1, Args_size=1    0:   getstatic       #16; //Field java/lang/System.out:Ljava/io/PrintStream;    3:   ldc     #22; //String hello world    5:   invokevirtual   #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V    8:   return   // Debug info snipped } 

Ok, so on line 3 we see a pushing of the "hello world" constant onto the stack via #22, but const #23 seems to hold the actual value. I guess I am a little confused with what the #(number) means when it appears on the right-hand-side of the printout.

Oracle/Sun's man page for javap leaves much to be desired.

like image 835
Andrew White Avatar asked Apr 05 '11 01:04

Andrew White


People also ask

What is constant pool?

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.

What is constant string pool?

A string constant pool is a separate place in the heap memory where the values of all the strings which are defined in the program are stored. When we declare a string, an object of type String is created in the stack, while an instance with the value of the string is created in the heap.

What is Javap?

DESCRIPTION. The javap command disassembles one or more class files. Its output depends on the options used. If no options are used, javap prints out the package, protected, and public fields and methods of the classes passed to it. javap prints its output to stdout.

How do you name a constant class?

We make our constants static and final and give them an appropriate type, whether that's a Java primitive, a class, or an enum. The name should be all capital letters with the words separated by underscores, sometimes known as screaming snake case.


1 Answers

All your class, interface, field names and string constants go into the java constant pool.

As per VM Spec ( http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html ):

The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte. The constant_pool table is indexed from 1 to constant_pool_count-1.

So in terms of constant pool something like below can be viewed as:

const #22 = String      #23;    //  hello world const #23 = Asciz       hello world; 

The value at #22 (index 22) is of type String and its value is null terminated c string (Asciz) hello world is at index 23.

like image 113
Favonius Avatar answered Sep 21 '22 17:09

Favonius