Skip to content
On this page

First Java program

Hello World

Let's write the first Java program.

Open a text editor and enter the following code:

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

In a Java program, you can always find one like:

java
public class Hello {
    ...
}

This definition is called class, where the class name is Hello case-sensitive, class used to define a class, public indicating that the class is public.

public , class are Java keywords, must be lowercase, Hello is the name of the class, according to custom, the first letter H should be capitalized. {} In the middle of the curly braces is the definition of the class.

Notice that in the definition of the class, we define a method called main:

java
public static void main(String[] args) {
        ...
}

A method is an executable block of code, a method in addition to the method name main , there is also a () method argument in parenting, where the main method has a parameter, the parameter type is , String[] the parameter name is args , public , is static used to modify the method, here it means that it is a public static method, is the return type of the method, void and curly braces {} In the middle is the code for the method.

Methods are used for each line of code; In the end, there's just one line of code here, which is:

java
System.out.println("Hello, world!");

It is used to print a string to the screen.

Java specifies that a class public static void main(String[] args) defines a fixed entry method for a Java program, so a Java program always starts with a main method.

Note that the indentation of the Java source code is not necessary, but after using the indentation, the format looks good, and it is easy to see the beginning and end of the code block, and the indentation is usually 4 spaces or a tab.

Finally, when we save the code as a file, the file name must be , Hello.java and the file name should also be case-sensitive, because it must be Hello exactly the same as the class name we defined.

How to run a Java program

Java source code is essentially a text file. We need to first use javac to compile Hello.java into a bytecode file Hello.class , and then use java command to execute this bytecode file:

  • Hello.java -> Hello.class -> Run on JVM

Therefore, the executable file javac is the compiler, and the executable file java is the virtual machine.

The first step is to execute the command javac Hello.java in the directory where Hello.java is saved:

sh
$ javac Hello.java

If the source code is correct, the above command will not produce any output, and a Hello.class file will be generated in the current directory:

sh
$ ls
Hello.class	Hello.java

The second step is to execute Hello.class and use the command java Hello :

sh
$ java Hello
Hello, world!

Note: The parameter Hello passed to the virtual machine is the class name we defined, and the virtual machine automatically finds the corresponding class file and executes it.

Some children may know that it is also possible to run java Hello.java directly:

sh
$ java Hello.java 
Hello, world!

This is a new feature in Java 11, which can directly run a single file source code!

It should be noted that in actual projects, a single Java source code that does not rely on third-party libraries is very rare. Therefore, in most cases, we cannot directly run a Java source code file because it needs to rely on other libraries.

Summary

A Java source code can only define one public type class, and the class name and file name must be exactly the same;

Use javac to compile .java source code into .class bytecode;

Use java to run a compiled Java program, the parameter is the class name.

First Java program has loaded