Depth understanding of Java HelloWorld

HelloWorld Java programmer knows that each program. It is very simple, but simple guide you can begin to understand something more complex. This article will explore this HelloWorld something from this simple program that can be learned. If you have a unique understanding of HelloWorld, welcome to leave your comments.

 

HelloWorld.java

 

public class HelloWorld {

 

    /**

     * 

     * @param args

     */

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        System.out.println("Hello World");

    }

 

}

 

1, Why is everything from class start?

 

Java programs are built from the beginning of the class, each of the methods and fields in the class must be inside. This is due to the object-oriented characteristics of Java: everything is an object, it is an instance of the class. There are many advantages compared to functional programming language object-oriented programming languages, such as better modularity, scalability and so on.

 

2, Why there is always a "main method"?

 

The main method is the entry of a program, and is a static method. The static keyword means that the method is part of a class, rather than part of the object instance. Why is this so? Why we do not have a non-static method as the entry program it?

 

If a method is not static, then you can use this method after the object needs to be created. Because this method must be invoked on an object. For an entry, this is unrealistic. Thus, the inlet is static method procedure.

 

Parameters "String [] args" show an array of strings can be passed to the program to help program initialization.

 

3, HelloWorld program bytecode

 

To execute the program, Java files are first compiled into Java bytecode is stored into .class files. So bytecode what looks like? Byte code itself is unreadable, if we use a binary editor to open, so it looks like the following:

 

 

In the above bytecode, we can see a lot of operation code (such as CA, 4C, etc.), each of them has a corresponding mnemonic (aload_0 such as the following example). Operation code is unreadable, but can be used to view javap mnemonic form of .class files.

 

Performing "javap -c" method for each class may output the disassembly. I.e. disassembly instructions into Java bytecode.

 

javap -classpath .  -c HelloWorld

 

Compiled from "HelloWorld.java"

public class HelloWorld extends java.lang.Object{

public HelloWorld();

  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 Hello World

   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V

   8:   return

}

 

The above code contains two methods: a compiler inferred default constructor; the other is a main method.

 

Next, each method has a series of instructions. For example aload_0, invokespecial # 1, and so on. The function of each instruction can be found concentrated in Java bytecode instructions, e.g. aload_0 used to load a reference to a local variable stack from 0, getstatic class used to obtain a value of the static field. It may be noted after getstatic command "# 2" points to the runtime constant pool. The constant pool is one of the data area JVM runtime. We can see through the constant pool "javap -verbose" command.

 

Further, each instruction starts from a digit, such as 0,1,4 like. In the .class files, each method has a corresponding array of byte code. These numbers correspond to the subscript of each opcode memory array and its parameters. Each opcode is one byte length, and the instructions may have zero or more parameters. This is the reason why these numbers are not contiguous.

 

Now, we use "javap -verbose" command to further observe the class.

 

javap -classpath . -verbose HelloWorld

 

Compiled from "HelloWorld.java"

public class HelloWorld extends java.lang.Object

  SourceFile: "HelloWorld.java"

  minor version: 0

  major version: 50

  Constant pool:

const #1 = Method   #6.#15; //  java/lang/Object."<init>":()V

const #2 = Field    #16.#17;    //  java/lang/System.out:Ljava/io/PrintStream;

const #3 = String   #18;    //  Hello World

const #4 = Method   #19.#20;    //  java/io/PrintStream.println:(Ljava/lang/String;)V

const #5 = class    #21;    //  HelloWorld

function () {// foreign currency with a single www.gendan5.com

const #6 = class    #22;    //  java/lang/Object

const #7 = Asciz    <init>;

const #8 = Asciz    ()V;

const #9 = Asciz    Code;

const #10 = Asciz   LineNumberTable;

const #11 = Asciz   main;

const #12 = Asciz   ([Ljava/lang/String;)V;

const #13 = Asciz   SourceFile;

const #14 = Asciz   HelloWorld.java;

const #15 = NameAndType #7:#8;//  "<init>":()V

const #16 = class   #23;    //  java/lang/System

const #17 = NameAndType #24:#25;//  out:Ljava/io/PrintStream;

const #18 = Asciz   Hello World;

const #19 = class   #26;    //  java/io/PrintStream

const #20 = NameAndType #27:#28;//  println:(Ljava/lang/String;)V

const #21 = Asciz   HelloWorld;

const #22 = Asciz   java/lang/Object;

const #23 = Asciz   java/lang/System;

const #24 = Asciz   out;

const #25 = Asciz   Ljava/io/PrintStream;;

const #26 = Asciz   java/io/PrintStream;

const #27 = Asciz   println;

const #28 = Asciz   (Ljava/lang/String;)V;

{

public HelloWorld();

  Code:

   Stack=1, Locals=1, Args_size=1

   0:   aload_0

   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V

   4:   return

  LineNumberTable: 

   line 2: 0

public static void main(java.lang.String[]);

  Code:

   Stack=2, Locals=1, Args_size=1

   0:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;

   3:   ldc #3; //String Hello World

   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V

   8:   return

  LineNumberTable: 

   line 9: 0

   line 10: 8

}

References Description JVM specification: runtime constant pool provides functionality similar to the symbol table of the role of traditional programming languages, although it contains content than conventional symbol table wider range.

"The invokespecial # 1" instruction "# 1" points to the constant pool # 1 constant. This constant is "Method # 6. # 15;". With this figure, we can recursively get the final constants

LineNumberTable provided to indicate correspondence information between source code and the Java bytecode instructions for the debugger. For example, line 9 Java source code corresponding to the main method bytecode 0, and line 10 corresponds to the 8 byte code.

If you want to learn more about the byte code, you can create a more complex class compile and view, HelloWorld really just the beginning.

4, HelloWorld in the JVM is how to implement it?

The question now is how the JVM load the class and call the main method?

Before the main method of execution, JVM needs to load , link and initialize the class.

  1. Loading the class / interface loaded binary form JVM. 

  2. Link will integrate into the binary type of data the JVM runtime. Link consists of three steps: verification, preparation and resolution (optional). Verification ensures that class, the interface structure is correct; involves preparing a class, allocating memory needed interface; resolve symbolic references are resolved. 

  3. Finally, the correct initial value of the initial assignment for class variables.

Work is loaded by the Java class loader to complete. When the JVM startup, will use the following three class loader:

  1. Bootstrap class loader : load at the core Java class libraries in the / jre / lib directory. It is part of the core of the JVM, and the use of native code compilation. 

  2. Extension class loader : loading code directory (such as / jar / lib / ext) extension. 

  3. System class loader : the code loaded on the CLASSPATH. 

Therefore, HelloWorld class is loaded by the system loader. When the main method is executed, it will trigger other dependent classes loaded, linked and initialized. Provided they already exist.

Finally, main () is pressed into the JVM stack frame, and the program counter (PC) was also appropriate settings. Then, PC indicates println () JVM stack frame is pushed into the stack. When the main () method is finished it will be popped off the stack, bringing the execution process is over.

Reference Documents

  • load 

    http://docs.oracle.com/javase/tutorial/ext/basics/load.html 

  • Class loading mechanism 

    http://www2.sys-con.com/itsg/virtualcd/java/archives/0808/chaudhri/index.html 

  • Class loader 

    https://en.wikipedia.org/wiki/Java_Classloader

Guess you like

Origin www.cnblogs.com/gendan5/p/11759824.html