Hello World Program in Java

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Hello World Program in Java

The first program a beginner writes in Java is the Hello World Program. However, on many occasions, we do not take into account the details of basic syntax. In this article, we discuss the details of the Hello world program in Java.  We will be covering the topics like Hello world program, analysis of syntax, how to compile the program, and how we can execute it. So, let's begin our article. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. 

Hello World Program in Java

Before we start curating the details, let's first have a look at the coding and check how the basic hello world program in Java can be coded.

public class demo {

    public static void main (String[] args) {

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

    }

}

And that’s the first program written by most of the beginners. Hence let’s analyze this program syntax in detail.

 

Syntax Analysis

Public class Demo {

Here through the use of the Class keyword, we have declared a new class known as Demo. We all know that Java is an object-oriented programming language (OOPS), and the whole class definition including the members must be enclosed within the curly braces. Also, we have used the public keyword, and that means the class is accessible from outside the package as well. If we make use of the other scope specifiers like Private or Protected that will affect the accessibility of the class. And that we will see in a separate blog. Please do not worry about it for now. 

Second Line: public static void main (String[] args){

This line has a method known as main(String[] args). This is the main method. It’s the actual entry point for the Java compiler. And it starts the program execution from here onward. And hence, whenever we execute a program, the first method invoked is the main method. And various other methods are invoked from inside the main method. And in all standard Java applications, we must have one main method as the program execution is triggered through it.

Let’s now discuss each word of this line.

Public: This is an access modifier. And it sets the visibility. And since it's public, the JVM can execute the main method from anywhere.

Static: This keyword makes the class static. And the main method Is declared static as we don’t need to define an object to call the main method. It gets invoked by JVM without object creation. And this saves the memory.

Void: This represents the return type of the method. The java main method never returns any value and hence it's always declared void.

Main(): This is the name of the method configured in JVM. 

String[]: This means that the main method can take in a single line argument which is of a type string array. And it's the java command line arguments. There are various other main method signatures possible. And they are:

static public void main(String[] args)

final public static void main(String[] args) 

And there are various others possible. We will discuss them in a later blog post. Please do not worry about it hence, for now.

Third line: System.out.println(“Hello World”);

system: This is in Java.lang package, and is a pre-defined class that has various variables and methods.

out: This is of type Printstream and is a static member field.

println: This method is in PrintStream class and we use it for printing the arguments which are passed to the console, and it also prints a newline. You can omit this and use the print() method if you do not want a newline.

We can also make use of System.exit() here. And if we pass 0 to the exit method, it means successful termination. And 1 means unsuccessful termination.  This is available in java.lang package.

And that completes our analysis of our first Java program. Let's now discuss how we can compile the Hello world Java program.

Compiling the Program

You first require to type this program in a text editor and save it. The file name will be the class name that you have mentioned in your program... Here it will hence be demo.java.

And in the next step, we need to open the console window, and then we need to navigate to the directory where the program is saved.  For compiling the program we need to make use of the command:

javac demo.java

You need to understand that java is case sensitive, and hence make sure that you are typing the file name in correct format.

If the program is executed successfully, then a demo.class file gets generated, and this is independent of the machine and also portable by nature.

And now that we have done the program compilation successfully, we need to execute the Hello world program in Java for getting the output.

Executing the Program

For executing the Hello World java program via the command line, we require typing the below code, which is 

java demo

Congratulations, you have executed your first Java program successfully. 

If you are using any IDE, you can omit all these steps, and just tap on execute button there, and execute your first java program.

And that finishes our article on the first program written by any beginner in Java. If you want to learn more you can join our Java course. And for that, you can contact us anytime and from anywhere in the world.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let's have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.