Summary: in this tutorial, you’ll learn how to develop your first Java program called “Hello, World!” that outputs the “Hello, World!” message to the screen.
Creating the Java Hello World Program
First, create a new directory called HelloWorld
in your hard drive e.g., D:\
which stores the Java source code.
Second, open your favorite text editor and create a new file called App.java
with the following contents:
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Code language: Java (java)
Third, open the Command Prompt on Windows or Terminal on macOS and navigate to the HelloWorld
directory.
Fourth, compile the App.java
to the App.class
by using the javac
command:
javac App.java
Code language: Java (java)
If you view the contents of the HelloWorld
directory, you’ll see a new file called App.class
. It is the result of compiling the App.java
file.
Finally, compile the App.class
into native machine code and run it using the java
command:
java App
Code language: Java (java)
You’ll see the following output:
Hello, World!
Code language: Java (java)
Notice that you do not add the .class
to the App
when you use the java
command.
Understanding how the Java Hello World program works
Every Java program contains at least one class. For example, the Hello World program has the App
class.
By convention, the name of the class is the same as the filename. In our example, the App
class is the same as the file name App.java
.
public class App {
// ...
}
Code language: Java (java)
The App
has a static method called main()
.
The main()
method is the entry point of a Java program. In other words, when you execute a Java program, Java looks for the main()
method and executes it first:
public class App {
public static void main(String[] args) {
//...
}
}
Code language: Java (java)
Inside the main()
method, we have a statement that displays the message "Hello, Word!"
to the screen:
System.out.println("Hello, World!");
Code language: Java (java)
The statement uses the println()
method of System.out
. The println()
method prints a text string surrounded by double quotes "
to the screen.
Building the Hello World using an IDE
In practice, you rarely build a Java program using a plain text editor. Instead, you’ll use a tool called IDE.
IDE stands for an integrated development environment. It is a software application that helps you develop software code efficiently.
Typically, an IDE provides you with the following features:
- Syntax highlighting
- Code formatting / auto-completion
- Debugging tools
- Build, compile, and run the code within the IDE
- Code refactoring and testing
There are some popular Java IDEs such as Eclipse, IntelliJ, and Netbeans. We’ll use the IntelliJ IDE in this tutorial.
IntelliJ IDE has both paid and community versions. And we’ll use the IntelliJ IDE community version.
Installing IntelliJ IDE
First, download the IntelliJ IDE from this link.
Second, follow the instructions to install it on your computer.
Third, once installed successfully, you can launch it.
Creating Java Hello World Project
IntelliJ IDE manages Java programs as projects.
First, click the New Project button to start creating a new project.
Second, enter the name of the project e.g., HelloWorld
and the location where you want to store the project e.g., D:\java
.
Notice that IntelliJ automatically detects the JDK on your computer.
Third, once the project is created, you can add Java classes to the src
directory, which stands for source, as listed on the Project structure:
Right-click the src
directory and select New > Java
Class:
It’ll show a small window that prompts you to choose a class name, you enter the class name as App
and press Enter:
Once the IntelliJ created the App
class, you can enter the source code in the code editor.
Fourth, build, compile, and run the program by clicking the Run button:
If the code is valid, you will see the output of the program in the output panel as shown in the following picture:
Behind the scenes, IntelliJ runs the javac
command to build the App.java
file to the App.class
and stores it in the out/production/HelloWorld/
. It then executes the java
command to compile and run the App.class
Summary
- A Java program has at least one class. By convention, the class name is the same as the file name.
- A Java program always executes the
main()
method first. - Use the
System.out.println()
to output a message to the console.