Written by - Yash Agrawal

Hello Word java program


In this we will see how to write Hello World in java and understand every build in syntax. In java it is important to have a main method to indicate the starting point.

The requirement for Java Hello World Example:
  • Install JDK on your machine, to download click
  • Set path of the jdk/bin directory.
  • Create a JAVA program and save it with .java extension.
  • Compile and run the program.

Example code:
class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello World");  
    }  
}  
Write this to compile on terminal where Simple.java is the file name javac Simple.java then to execute write java Simple. When you compile the code .class file is created which is the bytecode and then you run that class.

Now lets understand every single syntax in this program.

  1. class keyword is used to declare a class in Java.
  2. public keyword is an access modifier that represents visibility. It means it is visible to all.
  3. static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory.
  4. void is the return type of the method. It means it doesn't return any value.
  5. main represents the starting point of the program.
  6. String[] args or String args[] is used for command line argument. We will discuss it in coming section.
  7. System.out.println() is used to print statement. Here, System is a class, out is an object of the PrintStream class, println() is a method of the PrintStream class. We will discuss the internal working of System.out.println() statement in the coming section.

Resolving the error "javac is not recognized as an internal or external command"?

If this error shows up while compiling the code this means that the path is not set correctly beacuse of that DOS is not able to identify javac.