Hello World Java – A Detailed Explanation

Start learning Java programming with Hello World Java Program.

From this tutorial, we will start learning all essential Core Java Concepts including Java Fundamentals, OOPs, Multi-Threading, Collections, File Handling, Exception Handling, and some other Core Java Topics with Complete explanations and Logic behind the code. But first thing first-

Start Learning Java - Hello World Java Program

A “Hello, World!” is the most basic and simplest program that outputs “Hello, World!” on the screen. Since it’s a very simple program, it’s highly used to introduce a new programming language to a newbie, in this case – Core Java.

Let’s start learning how “Hello, World!” Java program works behind the code.

But to run Java programs on your computer, Make sure that you have properly installed Java. Also, you need an IDE (Integrated Development Environment like Eclipse) to compile, run and debug your Java programs. Or at least a Text editor (like Notepad++) to write and edit the Java code. For that, you can check- How to download and run Java on your computer?

Explaining: Hello World Java

Here is how to write a Hello, World! Java Program

// The First Program...
public class TheHelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!!");
    }
}

Output:

Hello, World!
Note***

If you have also written the exact code, you need to save the file as TheHelloWorld.java. The reason is, in Java, the name of a public class and the name of the .java file name must be matched.

  • Technically speaking: the filename should have the same name as the public class name in that file, which is a way to tell the Java Virtual Machine (JVM-) that this is what the entry point is for you.
  • JVM: Java Virtual Machine is an essential software component that interprets the .class file (well known as byte code) into the “Machine understandable code” depending upon your operating system and hardware combination.

Don’t worry, If you’re not getting a few of the terms used here, just note them down, and keep learning further as we’ll understand all of them in later tutorials.


Hello World Java Program Diagrammatic Tutorial for Beginners


Logic: How does “Hello, World!” Program work?

Step 1) // The First Program…

What you are seeing above is a comment in Java. Comments are also part of the program, but these are completely ignored by Java compilers. Their main use is to help programmers to understand the code.

Another type of comment in Java is Multi-Line Comments:

/* An example of multi-line comment in Java.
 * We are learning "Hello, World!" Java Program with standard output.
 */

Step 2) class TheHelloWorld { … }

In Java programming, this is how we define a class. Every Java application has a class definition, and the name of the public class should match the filename in Java as we already learned above.

Technically speaking, a class is a blueprint from which objects are created. Here’s what a class contains-

  • Fields: To hold some values
  • Methods: To perform operations
  • Constructors: To initialize its Object
  • Blocks: Provides initial values (before even creation of objects)
  • Nested class and interfaces: For specific needs (that we’ll see later).

Here in the above Hello world Java Program, we only had a static method and that’s main().


Step 3) public static void main(String[] args)

Every single word in the public static void main statement has a different meaning for the JVM. So, let’s understand public static void main(String[] args) meaning:

  • public- It’s basically an Access Modifier, which tells Java, from where and who can access the method. Defining the main() method public makes it globally available. public allows JVM to invoke it from outside of the class as it is not present in our class TheHelloWorld.
  • static: It is a keyword in Java that converts a method into a class-level method. The main() method is static so that JVM can invoke it without worrying to create an object of our class.
  • void: It is also a keyword in Java that specifies, a method will not return anything from where it was called.
  • main: It is just the name of Java’s main method. You can think of it as a hard and fast rule defined by the Java Development Team, that indicates JVM to find out the starting point of the Java program. By the way, It’s not a keyword.
  • String[] args: It is of 2 sub-parts- String[] and args. The String[] has been put inside the main( … ) which tells Java that the main(…) method is taking a String array type of arguments whenever invoked. Here, the name of the String array is args but it is not fixed and you can use any name in place of it, such as- “String[] TheArguments”.

The brackets [] describe that the object is of type Array. In Java, these brackets can be put before or after the argument name like- String[] TheArguments or String TheArguments[].

For now, just remember that, In Java programming, the main method is the entry point of your Java application, and it’s mandatory in any Java program. The signature of the main method can not be modified (except what we’ve seen above) and its General syntax layout is:

public static void main(String[] TheArgs) {
... /* Java Code */ ...
}

Step 4) System.out.println(“Hello, World!”);

The above statement will print “Hello, World!” in the console (/terminal). Here’s a detailed explanation of System.out.println in Java-

The System.out.println() statement used in the above Hello World Java program will take an argument and print it. The statement can be further broken into 3 parts which can be better understood separately as:

  • System: It is a final class defined inside the java.lang package.
  • out: This is an instance of PrintStream type, that’s public and static member variable of the System class.
  • println(): As all instances of PrintStream class have permission to invoke a public method println(), hence we can invoke the same method on out as well. By the way, it’s an upgraded version of method print(). The method println() prints any argument (whether it’s String, Integer, Float or any other) passed to it and adds a new line to the output.
Interview Tip***
Explaining a Hello World Program and System.out.println() are Common Core Java Interview Questions. Make sure, you have a strong command to explain every single bit of it.

What Next? 

We have learned How to write a Hello World Java Program. Also, we’ve understood each and every part of it such as System.out.println() and public static void main() in Java.

Don’t worry if you don’t understand some of the meanings like- final, static, and so on for now. We will learn all such Core Java Concepts in our next tutorials.

To learn other Important Core Java Concepts, Must check these tutorials.

Leave a Comment