Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Inside the Hello World Application via Java Class Viewer

, 23 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Analysis the "Hello World" application at binary (JVM) level

Introduction

The Hello World Application is the first application for everybody opens the program world. Here we describe the secrets inside the Hello world application. And this will help you to understand the Java programming in depth.

The Hello World Application

Here is an example of Hello World application comes from The Java Tutorials:

/**
 * The "Hello World!" Application.
 * <p>
 * This application comes from: 
 * http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html .
 * </p>
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Here is the analysis result of the corresponding class file HelloWorldApp.class:

Inside the Hello World Application

The Constant Pool

The constant pool is a table of structures representing various components in the class file, like string constants, class and interface names, field names, etc. It includes:

  • Class
  • Fieldref
  • Methodref
  • InterfaceMethodref
  • String
  • Integer
  • Float
  • Long
  • Double
  • NameAndType
  • Utf8
  • MethodHandle
  • MethodType
  • InvokeDynamic

Although the 'Hello World' application is so simple to contain only 1 line of code, the constant pool of this class contains 34 items, as the following screen shot shows:

The following screen shows the extracted data (human readable) of the constant pool:

  1. The first part shows the 'tag', it is the type of the constant pool object
  2. The rest shows the value of the constant pool object
Where is the text of 'Hello World!'?

The 'Hello World!' string is contained in constant pool item 3 (tag/type: String), and the item 3 is redirected to item 23 (tag/type: Utf8):

The Methods

Although there is only one main method is defined in the HelloWorldApp.java source code, 2 methods exist in the class file:
  1. void <init> ()
  2. public static void main (java.lang.String[])
When compile, the javac tool generated one method named <init> .

The Method: <init>

This method is used to initialize the object. If no constructor is defined in the class source code, the method <init> will be generated also. By default, the method will call the java.lang.Object.<init> method directly.

Here is the source code of the method in binary format, it has 5 bytes:

 2A  B7  00  01  B1 

Here is the extracted source code (technically readable) of the <init> method. The code is in opcode format, and the Java Class Viewer adds the description of the binary code:

There are 3 commands in the opcode:

1. aload_0

Load the 0th variable local variable table, it is a reference to a Java Object Instance. In our case, it loads the this object from the local variable.

There are similar opcode instructions to aload_n:

aaloadLoad reference from array
aloadLoad reference from local variable
aload_nLoad reference from local variable, The n must be an index into the local variable array of the current frame
baloadLoad byte or boolean from array
caloadLoad char from array
daloadLoad double from array
dloadLoad double from local variable
dload_nLoad double from local variable
faloadLoad float from array
floadLoad float from local variable
fload_nLoad float from local variable
ialoadLoad int from array
iloadLoad int from local variable
iload_nLoad int from local variable
laloadLoad long from array
lloadLoad long from local variable
lload_nLoad long from local variable
saloadLoad short from array

2. invokespecial

Call the instance method <init> of super class java.lang.Object.

3. return

Return void from current method.

The Method: main

This is the main() method compiled from the source code.

Here is the source code of the method in binary format, it has 9 bytes:

 B2  00  02  12  03  B6  00  04  B1 

Here is the extracted source code (technically readable) of the main() method. The code is in opcode format, and the Java Class Viewer adds the description of the binary code:

There are 4 commands in the opcode:

1. getstatic

Get the static field out from class java.lang.System.

2. ldc

Push item 3 from run-time constant pool; the tag (type) of item 3 is CONSTANT_String_info, and it refer to item 23, whose type is CONSTANT_Utf8_info.

As described above, the value of item 23 is 'Hello World!'.

3. invokevirtual

Invoke the instance method java.io.PrintStream.println.

4. return

Return void from current method.

The Attribute of the Class

Attributes are used in the Class, Field, Method, and Code objects of the class file in JVM.

In our case, there is an SourceFile attribute of the HelloWorldApp class.

This attribute refer to item 19 of constant pool, and is value is 'HelloWorldApp.java', specifying the source code file name of current class.

History

  • 2014-04-22 Created this article.
  • 2014-04-24 Fist version of this article.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

AmosShi
Software Developer (Senior)
United States United States
Deliver useful software to the world.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 24 Apr 2014
Article Copyright 2014 by AmosShi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid