English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)/O)

Java Reader/Writer

Other Java topics

Java Entry Program

In this tutorial, you will learn to write a "Hello World" program in Java.

The "Hello, World!" is a simple program that outputs on the screen. Since it is a very simple program, it is often used to introduce a new programming language to beginners.

Let's explore the Java "Hello, World!" program.

To run this program on your computer, make sure Java is installed correctly. In addition, you need an IDE (or a text editor) to write and edit Java code.

Java "Hello, World!" program

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

If you copy this code, you need to save the filename as HelloWorld.java. This is becauseThe class name and filename should be the same in Java.

When the program is run, the output is:

Hello, World!

Java "Hello, World!" example program explains:

  1. // Your first program
    Any line in Java that starts with//are all comments. Comments are intended for users to read the code and better understand the intention and function of the program. The Java compiler (the application that converts Java programs into computer-executable Java bytecode) will completely ignore it. For more information, please visitJava comments.

  2. class HelloWorld { ... }
    In Java, every application starts with a class definition. In the program, HelloWorld is the name of the class, and the class definition is:

    class HelloWorld {
    ... .. ...
    }

    Now, remember that every Java application has a class definition, and the name of the class should match the filename in Java.

  3. public static void main(String[] args) { ... }
    This is the main method. Every Java application must include the main method. The Java compiler starts executing the code from the main method.
    How does it work?What a good question. However, we will not discuss it in this article. After all, this is the basic program that introduces the Java programming language to beginners. In the following chapters, we will learn about the meanings of public, static, void, and so on.How methods work?。
    Now, remember that the main method is the entry point of the Java application and is required in Java programs. The signature of the main method in Java is:

    public static void main(String[] args) {
    ... .. ...
    }
  4. System.out.println("Hello, World!");
    The above code prints the string 'Hello, World!' enclosed in quotes to the standard output (your screen). Note that this statement is located inside the main function, which is inside the class definition.

Note

  • Every valid Java application must have a class definition (matching the filename).

  • The main method must be within the class definition.

  • The compiler executes the code starting from the main function.

This is a valid Java program that does not perform any operation.

public class HelloWorld {
    public static void main(String[] args) {
        //Write code here
    }
}

If you are still not clear about the meanings of class, static, methods, etc., don't worry. We will discuss them in detail in the following chapters.