Chapter 02 of 57

First Java Program

Now that we know the basics of Java, it's time to write our first Java program. If you are completely new to programming, don't worry if the code looks a little strange at first. We will go through it step by step and understand what each part does.

Our First Java Program

Let's start with the classic first Java program:

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

When we run this program, it produces the following output:

Hello World!

It looks like a very small program, but there are a few important things inside it. Let's understand them one by one.

Creating a Class

The first line is:

class Main {

Here, we are creating a class named Main. A class is used to organize Java code, and our program is written inside this class. We will learn classes and objects properly later, so for now, just remember that Java code is generally organized inside classes.

The curly braces { } show where the class starts and ends. Everything between these braces belongs to the Main class.

The main() Method

Inside the class, we have:

public static void main(String[] args) {

This is called the main method. When we run a Java application, execution starts from the main() method. In other words, Java looks for this method as the starting point of our program.

You might be wondering what public, static, void, and String[] args mean. Don't worry about them right now. We will learn these concepts separately. For now, the important thing to remember is that the main() method is where our program starts executing.

Printing Something on the Screen

Now let's look at the most interesting line:

System.out.println("Hello World!");

This statement is used to print something to the console. Here, we are asking Java to display the text "Hello World!".

We can easily change the text:

System.out.println("Welcome to Java!");

The output will be:

Welcome to Java!

We can also print numbers:

System.out.println(100);

The output will simply be:

100

So, for now, you can remember that System.out.println() is used when we want to display something on the screen.

Why Do We Use a Semicolon?

You may have noticed the semicolon at the end of the statement:

System.out.println("Hello World!");

The ; tells Java that the statement has ended. Most Java statements require a semicolon at the end.

For example:

System.out.println("Hello");
System.out.println("Java");

Both statements have a semicolon at the end.

Forgetting the semicolon is a very common beginner mistake, so make sure you don't miss it.

Printing Multiple Lines

We can use System.out.println() multiple times to print multiple lines.

class Main {
    public static void main(String[] args) {
        System.out.println("Hello!");
        System.out.println("I am learning Java.");
        System.out.println("Java is fun!");
    }
}

The output will be:

Hello!
I am learning Java.
Java is fun!

Every time println() is used, Java prints the given value and then moves to the next line.

Java also provides System.out.print().

The difference between print() and println() is very simple. println() moves to a new line after printing, while print() keeps the cursor on the same line.

For example:

System.out.println("Hello");
System.out.println("Java");

Output:

Hello
Java

But:

System.out.print("Hello ");
System.out.print("Java");

Output:

Hello Java

So just remember:

print() → prints on the same line

println() → prints and moves to the next line

Try It Yourself

Now it's your turn. Try changing the program and printing some information about yourself.

For example:

class Main {
    public static void main(String[] args) {
        System.out.println("My name is Imran.");
        System.out.println("I am learning Java.");
        System.out.println("I want to become a software developer.");
    }
}

Don't just copy the example. Change the text and experiment with the program. Try adding more println() statements and see what happens. This kind of small practice will help you become comfortable with Java syntax.

Quick Revision

In our first Java program, we learned that Java code is written inside a class, and the main() method is the starting point of the program. We use System.out.println() to print something and move to a new line, while System.out.print() prints without moving to a new line. We also learned that Java statements commonly end with a semicolon ;, and curly braces { } are used to define blocks of code.

Don't worry if the main() method still looks confusing. We will learn each part gradually as we move through the Java tutorial.