Hello world program in Java

Programming in Java has always been fun for me. With this post I begin the Java programming tutorials journey. In this post, we will learn how to write a simple Hello world program in Java. We will learn basic structure of a Java program, how to write, compile and execute a Java program.

Hello world program in Java

Before we write our first Hello World program in Java, below are the prerequisites for the tutorial.

  1. Download and installed Java SDK (JDK).
  2. Configure Java PATH and CLASSPATH.

Once you are done with the Java configurations. Open your favourite text editor and let us write our first Hello World program in Java.

Hello world program

/**
 * Java program to print Hello World! 
 */
public class HelloWorld {

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

Once you are done, save it. Be careful, you must save your file with same name as of your class name. Means, here our file name should be HelloWorld.java.

Before we compile and run our first Java program. Let us first understand different programming elements we used in our first Hello World program.

Understanding basic structure of a Java program

  • /**
     * Java program to print Hello World! 
     */

    Very first line of our program is comment. Comments are non-executable part of program. We write comments to document our code for increased readability. It is used to describe a section or complex logic of a program.

  • public class HelloWorld {
    
    }

    Every Java program consist of a class. In Java everything is encapsulated inside a class. On a high level, class is an encapsulation of variables (often known as members) and methods.

    Here public class HelloWorld is class declaration.
    public is access modifier keyword. It specifies HelloWorld class as publicly accessible.
    class is also a keyword used to declare a class.
    HelloWorld is an identifier or class name.

    Every class has its own body, specified by a pair of curly braces { }.

  • public static void main(String[] args) {
       
    }

    Like other programming languages, main() method define entry point of a Java application. Every Java application has a main() method. main() is a special method, it tells JVM to start execution from here.

    Let us dissect main() method definition.
    public is access modifier. It means the method is accessible from anywhere (also by JVM).
    static is java keyword, used to call main() method without object creation.
    void defines return type of main() method. It specifies that main() does not return anything.
    String[] args is an array of string. It accepts command line arguments in args.

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

    println is a method used to print any message on console or standard output device.

    We will discuss later why we need to write complete System.out.println(); instead of simple println();.

Its time to compile and run our first Hello World program in Java. So without any delay lets run our first Java program.

How to compile and execute a Java program through command line?

After understanding basic structure of a Java program. Let us move ahead and compile our first Java program. To compile and execute you need to install and configure JDK (Java Development Kit).

Steps to compile and execute a Java program.

  1. Open terminal or command prompt.
  2. Navigate to the directory where you have saved your java program.
  3. To compile a java program, type javac your_file_name.java and hit enter. In our case it will be javac HelloWorld.java.

    If you have copied the HelloWorld example correctly, you will not see any message on compilation. Otherwise, for any compilation errors, java compiler will complain to fix and compile again.

    Once you successfully compiled your java program. Java compiler generates a .class file in same directory. Discussion about .class is beyond the context of this post. I will discuss about generated .class file in a separate post.

  4. After successful compilation, type java your_file_name and hit enter to run your java program. If everything goes well you will see following output on your console.

Output:

Hello World!

Happy coding 😉

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>