11

This is the source code I have:

public class Koray {
    public static void main(String [] args) {
            System.out.println("This is a sample program.");
    } 
}

And when I compile this, I get the bytecode. When I look at the bytecode with a Hexadecimal viewer I see part:

19 54 68 69 73 20 69 73 20 61 20 73 61 6D 70 6C 65 20 70 72 6F 67 72 61 6D 2E

which can be read as

This is a sample program. 

if the bytes are interpreted as characters.

And when I do

javap -c Koray.class

do disassemble this class I see:

Compiled from "Koray.java"
public class Koray {
  public Koray();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String This is a sample program.
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: bipush        10
      10: istore_1      
      11: return        
}

My question is, where is this String seen in the disassembled text? I only see it in a comment.

1
  • You have a lot of very nice questions! Commented Aug 30, 2018 at 19:01

3 Answers 3

5

See that ldc instruction? It loads a constant from the runtime constant pool. That's where the your string is stored. To print the constant pool as well, add -verbose option to javap call.

Sign up to request clarification or add additional context in comments.

6 Comments

Can we say that we should not use Strings in source code because it will fill up the memory when the class is loaded and will never be removed from the memory because it is in the constant pool?
@KorayTugay don't worry, interned Strings (and all String literals are interned) are stored in the heap since JDK7.
@KorayTugay Use them in the source code if it makes the code clearer than doing something else. The memory overhead of string constants is negligible.
@KorayTugay String literals end up in the "Perm Gen" part of non-Heap JVM memory. Perm Gen space is only examined when Full GC runs.
@KorayTugay The main reason to put strings in a configuration file rather than source code is not memory but internationalization. There are many ways to say "Hello world" but please don't hard code them all.
|
4
ldc #3; //String This is a sample program.

This line uses the opcode ldc, which loads a constant onto the operand stack. At this point, we're going to load whatever constant is in index #3 of our constant pool table.

The constant pool table is where most of the literal constant values are stored. (ref) The command javap -c -verbose <classfile> provides the output for constant pool table.

Example output:

const #22 = String      #23;    // This is a sample program 
const #23 = Asciz       This is a sample program;

More info from 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.

4 Comments

But what is in index 3?
Index 3 being the index in the table.
What table, can you be more specific?
javap -c -v classfile and you will see what is under index 3
4

ldc #3 loads the string constant stored in the constant pool, which is a separate table that stores constants such as String literals and class names.

See http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4 from the JVM specification:

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.

You can view the string table using javap -verbose ClassName.

A sample output would be something like:

Compiled from "Test.java"
public class tests.Test extends java.lang.Object
  SourceFile: "Test.java"
  minor version: 0
  major version: 50
  Constant pool:
const #1 = class        #2;     //  tests/Test
const #2 = Asciz        tests/Test;
const #3 = String        #4;     //  This is a sample program.
...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.