Java Hello World Program

0
70
Java Hello World First Program
Advertisement

Java Hello World program is a simple program that prints Hello World! in Console/ Output Screen.

Hence it is a very simple program, it’s often good to learn as programming language to a beginner.

Let’s understand how Java Hello World! program works.

If you want to run this program into your Computer then you should make sure you have properly installed the Java (i.e. JDK) into your Computer, also you need to install an IDE (Integrated Development Environment) or a Text Editor to write your program. If you haven’t install the JDK into your System then you can visit How to install JDK in your Computer?

Java Hello World Program

//First Java Program

class Hello_Java {

    public static void main(String[] args) {

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

    }
}

If you copied the exact code from the above window then save that file with the name of class of the Program i.e. Hello_Java.java because the class name and the filename should be the same in java. At the time of Compilation JDK generates a intermediate Byte code(.class) file which is the the same as the class name.

When you run the program you will get the output like shown below:

Hello World!

How Java “Hello World!” program works?

Java first compile the program and as we know compiler executes the program line by line and at the end show the error. here you can find the step by step explanation of Hello World! program:

1. //First Java Program

In Java any single line starts with two consecutive Back-slash(//), then that line is called as comment in Java Programs. Comments are used to explain the code to others which make programmer more understand about the code and functionality of the program.

The Comment line is ignored by the compiler at the run time, it is only used to read only in the Code editor.

2. class Hello_Java {...}

In Java, every valid program has a class definition. In this program, class is the keyword which defines the class and Hello_Java is the name of the class.

Syntax of class definition is:

class <Class_Name> {
    ....
}

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

The main method is the entry point of the Java Program. When compiler starts executing the java program the first control goes to the main method and then other statements and methods start executing. There should be only 1 main method with the signature defined above.

Syntax of Main Method:

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

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

The following code prints the string inside the quotation marks in the new line into your output screen. this method defines under the class under the main method.

Things to remember

  • Every java program must have a class name same as the filename. names are case sensitive in Java so the Class name and the file name should be matched.
  • The main method always defined under the class definition.
  • The execution always starts from main method.

Below are the valid Java program to print Hello World! :

//First Java Program

class Hello_Java {

    public static void main(String[] args) {

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

    }
}

This is the first program and beginning of Java programming if you are confused then don’t worry we will explain it in the later concepts. You can also comment below to ask you question regarding this post.

You could find the below post useful for your knowledge:

LEAVE A REPLY

Please enter your comment!
Please enter your name here