Java Hello World

In this lesson, we will learn to write a Java Hello World Program. This is the first program we will write in the course and it’s the first program anyone write once they start their learning of any programming language.Our Java Hello World program is a simple Java program that will produce an output of “Hello World!“. Without any delay let’s write our first Java program together.

1. Java Hello World

Here is our first Java Hello World program. I encourage you to write this code in your IDE and not copy paste to get the better feeling 🙂

Java Hello World
package com.javdevjoural;

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

Output

Hello World!
Congratulation on writing your first Java program!! How does it feel it run and see the output 🙂

2. How Hello World Works?

Now we have written our first program, let’s see how the program works

  • The first line is the package declaration. We will discuss it in the next lesson in more detail.
  • In Java every application start with a class definition. In our case our class is defined as HelloWorld.
    • public class HelloWorld{
  • Keep in mind that every Java application has a class definition and name of the class should match the file name. In our case you can’t name the file as MyFile.java while the class name id HelloWorld.java. We need to make sure the file name is also set as HelloWorld.java.

The public static void main(String[] args) is the main method. Every Java application must have a class with main() method. This is the entry point for our compiler to start executing the code.

Try to run a program without main() method and see what is the output? We will discuss the main() method in more details in the next lesson.

Summary

In this lesson, we wrote Java Hello World Program that is our first program in Java. As always the entire source code for this series is available on our GitHub repository.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.