Hello World: Your first program in Java


Keywords: Hello World, JDK, IDE, IntelliJ

Hello World Java

It’s time to write your first program in Java! It’s a tradition that a programmer’s first program in a new language print the text “Hello World!”. This article shows step by step how to download the IDE IntelliJ and how to write your first program.

How do I create my first program in Java?

Now you are finally going to write your first program in Java by following seven simple steps! The only thing our program should do is print the text “Hello World!”. Why “Hello World!”? Well, it is an old tradition that a programmer’s first program prints the words “Hello World!”.

The steps and images in this article are based on you using IntelliJ as your IDE, but you can, of course, take the corresponding steps in another IDE. If you have not downloaded any JDK or IDE yet, you can easily get started with our article: “Compiler, IDE, JDK and JVM in Java: This is what you need to start programming in Java“.

Before we start writing actual code in Java, you first need to have:

  • JDK (Java Development Kit) which we downloaded in Compiler, IDE, JDK and JVM in Java: This is what you need to get started.
  • IDE (Integrated Development Environment) where you write your code, for example, IntelliJ.

Step by step: How to create your first project in Java

How to get started with programming in Java

Step 1: Create a new project

First of all, start by launching the IntelliJ program and creating a new project.

Press create new project

IntelliJ Java New project

or use file > new > Project…

New project Java

Step 2: Select the Java Development Kit (JDK) you want to use

Under the heading “Software Development Kit (SDK)”, select the JDK you downloaded in the previous section. For example, choose JDK 1.8 or a newer version.

New Program Java Programming

Press next, and then press next again

New Program Java Programming

Step 3: Create a new Java file

Decide what the project should be called:

  • Do not use special characters such as%, &,! in your file and variable names.
  • A java file has the file extension .java, which indicates that the file contains java code. The text file where you write java code must be of the extension .java.
  • The file name cannot begin with a number, and it’s recommended to use English names.

In our case, we will name our project HelloWorld and then select the folder where we want to save our project. Since we are using IntelliJ, it automatically adds the .java file extension to our file.

Then press finish

Programming Java New Program

Step 4: Create a class

Start by right-clicking on the src (source) folder, then select new and finally Java Class.

Java Programming IntelliJ IDE

Step 5: Select the name for the class

You have now created a so-called java class where we can write our code. We will look a lot closer at what classes in Java are in later chapters, but for now, let’s just be content with creating the class.

Moreover, select the name that you want the class to have, and keep in mind that the same rules as the list for java file also apply here. For example, name the class file HelloWorld and press ok.

Create new class Java

Step 6: Start typing your code

Finally, now it’s time to write your first program in Java! Try writing the following code in the text editor:

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

Note the first line:

public class HelloWorld {

After public class, you must enter the same name as the class file. In the previous step, we created a class file named HelloWorld, which means that you must use the same class name here as well. Since we named the class file HelloWorld in our example, we also enter HelloWorld here.

Step 7: Try your program

Just right-click somewhere in the text editor and press the green arrow with the text “Run ‘HelloWorld.main ()’“

Run Code IntelliJ Java

Congratulations: You have now created your first program and are a programmer

You have now programmed your first program, a program that prints “Hello World” in the terminal.

Print program Java IntelliJ

And remember, since your first program was “Hello World!”, you could say that you are keeping the tradition of “Hello World!” alive.

To conclude, now you have hopefully created your first Java application that prints the text “Hello World!”. Feel free to reach out to us if something went wrong, or if you need help to get started. Additionally, we would gladly like to hear your ambitions and ideas why you started with Java programming, so feel free to send us an email with your ideas for your own Java project.

Summary: How to create your first program in Java

To write a program in Java, you need:

  • Make sure you have a JDK installed.
  • Download your favourite IDE where you write your code, such as IntelliJ or Eclipse.
  • Write your code in the text editor.
  • Run the program; the code is compiled and executed! You are now a programmer.

So far, it may be difficult to understand what the code actually does, but by going through chapter by chapter here on Code-Knowledge.com, you will gradually learn how everything works and interacts. As you are progressing and are diving into Java programming, you will notice how you can use the code to solve complex problems.

Frequently Asked Questions: Your first program in Java.

What is an IDE?

IDE stands for Integrated Development Environment, that is, a development environment where we can write code. There are many different IDEs to use and which one you choose is based on your preferences. In the article: “Compiler and JDK: This you need to start programming in Java“, we explain how an IDE works.

Which IDE can I use for Java programming?

There are several different IDEs, such as IntelliJ, Eclipse, BlueJ or NetBeans, but there are many, many more. Which one you decide to use is mostly about your preference and liking, or if your job or school has a predetermined IDE you should use.

How do I download the JDK?

To program in Java, you need a JDK (Java Development Kit). More about JDK and how to download it can be found in our article: “Compiler and JDK: This is what you need to start programming in Java“.