Java learning road-getting started, starting from the realization of Hello World!

Getting started, starting with the realization of Hello World!

Tall buildings rise from the ground!

Learning any programming language starts with the realization of a "Hello World!" My Java learning path also starts here...

1. Hello World program

First, create a new file with a .class suffix, and then enter the following code in it:

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("Hello World!");
    }
}

Open a command line window, go to the folder path containing the HelloWorld.class file, and run the following commands in sequence:

> javac HelloWorld.class
> java HelloWorld
Hello World!

Finally, you will see the final Hello World! output. This indicates that we have successfully implemented the first Java program! We have successfully stepped into the Java language event!

Two, program analysis

Let's do a simple analysis of this program:

  1. first row:

  2. public means public

  3. class means this is a class

  4. HelloWorld means that the name of this class is called HelloWorld

    The first line indicates the definition of a public class, the name of this class is called HelloWorld

  5. second line:

    1. public means public

    2. static means static

    3. Void represents the type of the function return value, void represents the function return value is empty , that is, no value is returned

    4. main represents the name of the function

    5. (String[] args) parentheses represent the parameter received by the function, the type of the parameter passed in is a string array, and the parameter name is args

      The second line indicates the definition of a public, static main method, the name of the method is main , and the return value is empty.

  6. The third row:

    Represents output to the console, the output is: Hello World!

Key summary:

  1. There can only be one public class in a xxx.java file , and the name of the class must be the same as the file name!
  2. " Public static void main " is the entry method of the Java program. When executing a Java program, the virtual machine will first look for this function. So this part is immutable
  3. " System.out.println("Hello World!"); " represents a Java statement. In a Java program, every sentence must end with a semicolon ";" or "{}" .

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/109896843