Java Hello World Program

Introduction

In this post, we will see how to write your first java program. This post is intended only for java beginners to acquaint them with the steps to write a simple java program. Simply put, we will write a code that outputs "Hello World!" to your console/screen.

For Folks having some knowledge in other programming languages, this is a basic program to print a statement on your console as you did earlier.

Prerequisite for running “Java Hello World Program”

This Java hello world program will be a very simple program that will print Hello, World! to console.

Before running the program, you need to make sure java is properly installed on your machine.

  • Install the JDK if you don’t have it installed, download the JDK and install it.
  • Set path of the JDK/bin directory. you can follow this link for setting the path

You can either write a program in IDE such as Eclipse or you can simply write it in text editor/Notepad save your code as a .java file. You can then compile java source code and execute your program in Command-Line. A detailed explanation is given below for this program.

You need to save this file as HelloWorld.java.

How does “Java program to print Hello World” works?

Let’s see a detailed explanation of the Hello world program.

Class declaration

This is a comment in java and this statement will be ignored at run time.
As java is object-oriented programming, every java application should have a class definition.

  • Class declaration: A class declaration includes the name and visibility of the class.class HelloWorld is a declaration of class that includes the keyword, followed by an identifier HelloWorld
  • The class declaration is followed by curly braces {}, which define the class’s definition.

Main method

This is called the main method in java. This is the entry point for this program.

public: This is an access modifier that is used to define visibility. Here the main method will be visible to all other classes.
static: static members belong to the class rather than any specific object. It simply means you can access the static members without creating any objects.
void: void is another keyword that defines the return type of the main method. It simply means the main method does not return anything before terminating the program.
main: main is the method name here.
String args[]: You can pass arguments to the java program using the args[] array. It is used to take user input using the command line.

Print statement

System.out.println is used to print literals in double-quotes("") to console. As we have passed “Hello, World!” here, it will print Hello, World! to console.

Semicolon

As you can see, each statement is terminated with a semicolon(;). You can put new lines or spaces in the code but the statement has to be ended by a semicolon.

Compile and run the program

If you run this program in eclipse ide, you can simply right click and on run as java application.
You can compile this java program using the command line as below:
Open the command prompt and go to the location where you have saved HelloWorld.java

$ javac HelloWorld.java

You can run the program using the command line as below:

$ java HelloWorld

When you run the above program, you will get the below output:

When we execute a java program, we need to give the full class name without .java extension.

In how many ways can you write a Java Program?

We can formulate different ways to write a Java program by doing some modifications to the main method’s signature. Below some examples are shown on how we can achieve this.

1. By Changing the Subscript Notation of Input Array in Main Method

We can change the position of subscript notation of the input array passed in the main method for command-line input. We mean the args[] array’s subscript notation can be used after Data type String, before or after the variable name.

To be more precise the above code for printing “Hello World!” can also be written in the following ways:

You see we have written the args array subscript notation before and after the variable name. The output for each case will be the same, we just change the signature of the main method.

2. By Changing the Sequence of Modifiers in the method, without modifying Method Prototype

We can also write the program in alternate ways by changing the sequence of modifiers and keywords. Consequently changing the method declaration.

In simple terms, we can interchange the position of keywords public and static, still the program would work fine. As the order of keywords does not matter. Likewise, we can write the above program as :

Note: The position of the return type keyword should not be mingled with. The return type of the method should always come after the modifiers and other keywords. Hence this an Invalid method declaration in java:

public void static main(String args[])

3. By Replacing Array Subscript Notation with 3 ellipses (Dots) for var-args support

We can also replace the Array Subscript notation in the args array and provide 3 dots instead for taking command line argument support. So instead of writing the main method like this: public static void main(String[] args)

We can alternatively write the whole program as:

Resolving Error: “javac is not recognized as an internal or external command”?

Suppose, while compiling the program shown in the above examples if you get the following error given below:

So, if you encounter this error, it means that the Command Line or DOS does not recognize the command javac. To resolve this issue we need to set the path of Java Resources to the bin Directory of your Java Development Kit. It is a good practice to set the path but it is not required if you save all your java source files inside the bin Folder.

Resolving Error: “reached end of file while parsing”

Again, while compiling any program due to some shallow mistakes, you might encounter the below given error:

This is a common error in Java and you need not worry about this. To resolve this error you just need to check whether all the Curly Braces i.e. { and } are opened and closed properly.

It is a good practice to check the braces while coding otherwise, for huge lines of code finding the missing braces can be a worrisome task.

Exercise

You need to print Yeah!! I executed my first java program on console and name of the class should be MyFirstJavaProgram

Important points

Let’s go through some important points about Hello world program.

  • Any java source file can have multiple classes but it can have only one public class.
  • Java source file name should be the same as the public class name. That’s why I said above “you need to save this file as HelloWorld.java“.
  • When you compile the java file, it is converted to byte code with .class extension, so you should be able to see HelloWorld.class in the directory.
  • When you execute the program, JVM looks for the main method and will execute it. You need to be very careful with the signature of the main method otherwise, the program may throw java.lang.NoSuchMethodError: main.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *