← Back to all posts

02 Java Fundamentals

Your First Java Program (Hello World)

By Dummy for Dummies


First Java Program

Write this in your text editor (e.g. VS Code) and run the program:

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

Output:

Console Output

Hello, World!

I wanted this to be your first program so I could stuff a few important things into your mind right away.

The first two lines and the last two lines are your magical spell. Write the first two lines at the start of your code, and the last two lines (two closing brackets) at the end. Don't worry about their meaning yet.

The third line, the one with System.out.println, is where you'll write most of your code for now. That's the line that actually does something. Swap "Hello, World!" for anything, and it prints that instead.

Here are 3 things you must understand from this program:


1. File Name vs Class Name

In the first line, the third word after public class must match your file name.

Capital and small letters matter. Banana and banana are different for Java.


2. The main Method

In the second line, you'll see the word main. This is the starting point of your program. Whenever you run code, Java looks for this keyword and starts executing from here.


3. Curly Brackets Around the Main Block

Right before the golden line, a { curly bracket opens. Right after it, a } curly bracket closes. Everything inside is called the main method, your main box of code for now.

You'll deal with other boxes later too, called methods, each enclosed in their own curly brackets. But your program always executes the main method first.


Closing

That's it for your first Java program! In the next blog, we'll understand Variables and Data types.

← Previous 01 - Introduction to Java Next → 03 - Variables and Data types in Java