The Java ‘Hello World’ Program

Java hello world is the first and most basic java program that we will go through. We would first write the program then compile & execute it. After execution, we would discuss it bit by bit. With that said, I hope you will love and enjoy Java.

Writing the Java Hello World Program

/**
 * This is our first program. It will only print the string
 * 'Hello World, welcome to Formulae Stack!'
 */
public class FirstJavaProgram {

	public static void main(String[] args) {
		System.out.println("Hello World, welcome to Formulae Stack!"); //displays the output

	}
}

  • First Create a folder named practiceCustom under any drive, in my case I have created it under D:\. Even though you can also choose to not create any folder but as a best practice, I would recommend you to create one. you can have any name, in my case I gave the name as practiceCustom.
  • Next, open your Notepad and write/add the above code. After this, save the program as FirstJavaProgram.java under practiceCustom folder.
  • Open the command prompt and go to the directory where you have saved the program. For our case, it should be D:\practiceCustom.
  • Type javac FirstJavaProgram.java and press Enter. This will compile your program. If there are no errors in your code then the command prompt will take you to the next line.
  • Type java FirstJavaProgram and press Enter to run your program.
  • You would see "Hello World, welcome to Formulae Stack!" printed on the command prompt.

Output

java hello world

Understanding the Program - Comments

The part under the /** */ and //  is the comment section. Comments are ignored by the java compiler and are not compiled. However, comments are very useful for programmers, developers and any learner who would look at the code. It helps any novice person understand important portions of the code. There are mainly three types of comments:

  1. Single line comment: Used to comment only a single line. For example, if you look at our program you would see the following comment: //displays the output
  2. Multi-line comment: Used to comment multiple lines of code. For example:
    /*
    This 
    is our
    first program
    */
  3. Documentation comment: Used to comment lines while creating documentation API. The javadoc tool is required to create documentation API. For example, if you look at our program you would see the following comment:
    /**
    * This is our first program. It will only print the string
    * 'Hello World, welcome to Formulae Stack!'
    */



Understanding the Program - Class

Per our program, the name of the class is FirstJavaProgram and always remember to save the files with the same name as the class name. It is worth noting that Java is case-sensitive.

The syntax of a class is:

accessSpecifier Class classname{
......
....

}

The keyword Class starts the definition of a class and all the code/logic for a class is present in between the curly braces. We would learn about class in details in the later part of the tutorial.

Understanding the Program - Main method

The main method is the only method from which execution of any class/java program starts and invokes other methods present in your application. It takes a single argument as an array of string. Each string in the array is called a command-line argument. It is a must that every java application must possess a main method. We would also study about the main method in detail later.

Understanding the Program - System.out.println()

This is used to display the result to the standard output i.e it displays the result into the console. It can also evaluate any expressions and display the value. The System is a predefined class. This topic is covered in detail in later part of the tutorial.