Java – “Hello World” program

Step 1:
We will create our first Java file, which can be done in any text editor (e.g. Notepad++, Atom, or Sublime Text).

Step 2: Below is the content of Hello World program file, all lines are required:

public class MyClass { public static void main(String[] args) { System.out.println(“Hello World”); } }

See Explanation of above program

Step 3:
Save the file as “MyClass.java” because our program name is MyClass. File name must be the same as the main function name.

Step 4:
Open a terminal (cmd, putty, git-bash), navigate to the directory where you saved your file.

Step 5:
Type following command in command line, to compile the code:

javac MyClass.java

In case if there is any syntax or code error, the command prompt will point it.

Step 6:
You will find an executable file “MyClass.class” in the directory where you saved your java program file.

Step 7:
Type following command in command line, to run the file:

java MyClass

It will show you the result of the file. In our case it is printing:
Hello World


Congratulations

Java – Explaination of Hello World program

public static void main(String[] args) { System.out.println(“Hello World”); }

Public:
Type: Access modifier
Description: In the above program using public makes main() method globally available.
Public method specifies from where and who can access the method. Public methods can be invoked by JVM (Java Virtual Machine) from outside of the class, in the same program file or any other java program file.

See Java Access Modifiers or Access Controls

Static:
Type: Keyword
Description: JVM invokes static main() method without instantiating the class which, also, in turn saves the unnecessary wastage of memory while calling object(s) declared for calling the main() method.

Void:
Type: Keyword
Return Type: Void
Description: Void is used to specify that a method doesn’t return anything.

main:
Type: Identifier
Description: JVM looks for main() as the starting point of the java program.

String[] args:
Type: Array of type java.lang.String class
Description: It stores Java command line arguments.

System:
Type: Class
Description: System is a final class in the java.lang package

out:
Type: Variable
Description: Out is a public and static member of the System class and is an instance of java.io.PrintStream

println:
Type: Method
Description: The println is a method of java.io.PrintStream, which prints any argument passed while adding a new line to the output.