[Simple Java] a deep understanding of the "HelloWorld" applet, simplehelloworld

Source: Internet
Author: User

[Simple Java] a deep understanding of the "HelloWorld" applet, simplehelloworld

For every Java programmer, HelloWorld is a very familiar program. It is very simple, but this simple code can guide us to understand some complex concepts. In this article, I will explore what we can learn from this simple code. If you have a unique understanding of HelloWorld, please leave your comment.

HelloWorld. java

public class HelloWorld {    /**     * @param args     */    public static void main(String[] args) {        System.out.println("Hello World");    }}
Why does everything start with a class?

Java programs are built based on classes. Each method and field must exist in the class. This is because Java is object-oriented: Everything is an object, that is, an instance of a class. Compared with functional programming, object-oriented programming has many advantages, such as more modular and better scalability.

Why is there always a "main" method required?

The main method is a static method and the entry of the program. The static method means that the method belongs to a class rather than an object.

So why? Why not use non-static methods as the program entry?

If this method is non-static, you need to create an object before using this method, because non-static methods need to be called by the object. As an entry to a program, such design is unrealistic. We cannot get eggs without chickens. Therefore, the program entry is set as a static method.

In addition, the input parameter "String [] args" of the main method indicates that a String array can be passed in to the method for program initialization.

HelloWorld bytecode

To run this program, Java files are first compiled into bytecode and saved to A. class file. So what is the bytecode file like? Bytecode itself is not easy to read. We open it in the hexadecimal editor and the result is as follows:

From the above bytecode, we can see a lot of operation codes (such as CA, 4C,), each of which corresponds to a mnemonic code (such as aload_0 in the following example ), the operation code is not easy to read, but we can use javap to view it. class file.

"Javap-c" can print the disassembly code of each method in the class. The disassembly code is some instructions, which specify the java bytecode.

Javap-classpath.-c HelloWorld

public class HelloWorld extends java.lang.Object{public HelloWorld();  Code:    0: aload_0    1: invokespecial #1; //Method java/lang/Object."<init>":()V    4: returnpublic 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 code above contains two methods: one is the constructor, which is automatically inserted by the compiler; the other is the main method.

There are a series of commands under each method, such as aload_0 and invokespecial #1. You can view the Java bytecode Instruction List for each specified meaning. For example, aload_0 loads the reference of the local variable in the stack and getstatic gets the static field value in the class. Note that "#2" after getstatic points to the runtime constant pool, which is the data area of the Java runtime. Therefore, using the "javap-verbose" command can help us to view the constant pool.

In addition, each command has a number, such as 0, 1, and 4. In the bytecode file, each method has a corresponding bytecode array. These numbers correspond to the index of the array, which stores the operation code and corresponding parameters. The length of each operating code is one byte and can have 0 or more parameters, which is why these numbers are not consecutive.

Now, we can use the "javap-verbose" command to take a closer look at this class:

Javap-classpath.-verbose HelloWorld

public class HelloWorld extends java.lang.ObjectSourceFile: "HelloWorld.java"minor version: 0major version: 50Constant pool:const #1 = Method #6.#15; // java/lang/Object."<init>":()Vconst #2 = Field #16.#17; // java/lang/System.out:Ljava/io/PrintStream;const #3 = String #18; // Hello Worldconst #4 = Method #19.#20; //java/io/PrintStream.println:(Ljava/lang/String;)Vconst #5 = class #21; // HelloWorldconst #6 = class #22; // java/lang/Objectconst #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>":()Vconst #16 = class #23; // java/lang/Systemconst #17 = NameAndType #24:#25;// out:Ljava/io/PrintStream;const #18 = Asciz Hello World;const #19 = class #26; // java/io/PrintStreamconst #20 = NameAndType #27:#28;// println:(Ljava/lang/String;)Vconst #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=10: aload_01: invokespecial #1; //Method java/lang/Object."<init>":()V4: returnLineNumberTable:line 2: 0public static void main(java.lang.String[]);Code:Stack=2, Locals=1, Args_size=10: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello World5: invokevirtual #4; //Methodjava/io/PrintStream.println:(Ljava/lang/String;)V8: returnLineNumberTable:line 9: 0line 10: 8}

The JVM specification is described as follows:

The runtime constant pool serves the method, similar to the symbol table in the conventional programming language, but the constant pool contains more data than the typical symbol table.

"Invokespecial #1" command "#1" points to #1 constant in the constant pool, that is, "Method #6. #15; "based on these numbers, we can recursively obtain the final constant. The row number table allows the debugging personnel to know which lines in the Java source code correspond to the instructions in the bytecode. For example, the first line in the Java source code corresponds to code 0 in the main method and the second line corresponds to code 8.

If you want to know

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.