Hello World

Table of Contents

Now, we are ready to learn how to execute a simple Hello World Java program. And you can do it in two different ways :

  • By using notepad installed on your computer.
  • Or, you can download a sophisticated text editor like Notepad++ which is highly interactive to be used by the beginners.

But in these tutorials, we will learn how to run it using notepad of your computer and Command Prompt.

Now follow the steps one by one.

Executing Hello World program in Java

Step 1

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

And now save the file as it is.

Step 2

Now, open Command Prompt and change the directory to the Desktop(as I have created the file in Desktop) or the location you have created the Hello.java file so that the JVM can locate the file stored.you can change the directory by just typing cd Desktop, and press Enter to execute the command.

Changing the directory to the Desktop
Changing the directory to the Desktop

Step 3

Now to compile the file you have to type the following command:

javac Hello.java
Compilation of Hello.java
Compiling the java file

Step 4

Now finally you have to run the compiled java file which will be created as soon as the hello.java file is compiled, in the same storage location.

Hello.class file is the Bytecode generated by JVM
Generated Bytecode ,which is the Hello.class

Now to run the file you have run in the name of the compiled file which is the class file created.

java Hello
Executing Bytecode or Class File
We have to execute with the name of the Class file generated.

Now, this is how you can execute a Java file. In the next section, we will decode each and every term used in the java file.

Let’s Decode the code

  • class keyword is used to declare a class in java.
  • public keyword is an access modifier that represents the scope of its usage.
  • static is a keyword. If we declare any method as static, it is known as the static method. The static method doesn’t need to create an object to invoke the static method. As the main method is executed by the JVM, so it doesn’t require to create an object to invoke the main method.
  • void is the return type of the method and here it means it doesn’t return any value.
  • main represents the starting point of the program which actually helps the JVM to identify from where it has to start the execution.
  • String[] args is used for the command-line argument.
  • System.out.println() is used to print the statement contained inside the inverted commas. Here, System is a class, out is the object of PrintStream class, println() is the method of PrintStream class. Here separating them by dots represents that they belong to the parent class on their left hand.

Previous How to Install Java JDK
Next Variables and Data-Types in Java

Leave a Reply