Java "Hello Word Program"

You will learn to write "Hello World" program in this article.



A "Hello, World!" is a simple program that outputs Hello, World! on the screen. Since it's a very simple program, it's often used to introduce a new programming language.

If you want to run this program in your computer, make sure that Java is properly installed. Also, you need an IDE (or a text editor) to write and edit Java code. For that, check How to run Java on your computer?

Java "Hello, World!" Program

 
 // Your First Program
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}
         
If you have copied the exact code, you need save the file name as HelloWorld.java. It's because the name of the class and filename should match in Java. 

When you run the program, the output will be:
hello word


How Java "Hello, World!" Program Works?


1  // Your First Program

In Java, any line starting with // is a comment. Comments are intended for person reading the code to better understand the intent and functionality of the program. It is completely ignored the Java compiler.


2  class HelloWorld { ... }

In Java, every application begins with a class definition. In the program, HelloWorld is the name of the class, and the class definition is:
class HelloWorld { 
... .. ...
}
For now, just remember that, every Java application has a class definition, and the name of class should match the filename in Java.

 3  public static void main(String[] args) { ... }

 This is the main method. Every application in Java must contain a main method. The Java compiler starts executing the code from the main method.

For now, just remember that main function is the entry point of your Java application, and it's mandatory in a Java program. The signature of main method in Java is:
public static void main(String[] args) {
... .. ...
}
4  System.out.println("Hello, World!");

The following code prints the string inside quotation marks Hello, World! to standard output (your screen). Notice, this statement is inside the main function, which is inside the class definition.

 Things to take away


  •  Every valid Java Application must have a class definition (that matches the filename). 
  • The main method must be inside the class definition. 
  •  The compiler executes the codes starting from the main function.

 This is a valid Java program that does nothing.
public class HelloWorld {
    public static void main(String[] args) {
        // Write your code here
    }
}





0 Comments