Java Program Basic | Hello World Program Explained for Beginners

Shape Image One

Java Program Basic | Hello World Program Explained for Beginners

Since we are thorough with the basics of Java now, it is time that we create our first Java program.

Yes, we’ll create the conventional Hello World program first to see how lines of code of Java look.  We further delve into the details explanation of some Java program basic questions.

You can create your first program once you have installed Java on your system. You can type the first program in Notepad, Notepad++, or any other text editor or code editor.

Hello World Program in Java

If it is your first program, open a basic notepad editor. And write the following code.

class Main
{
    public static void main (String args [])
    {
        System.out.println("Hello, World!");
    }
}
Note: The program is saved with the name same as that of main class with an extension ‘.java’.

Hence our program will be saved as Main.java.

That is it! Our first Java program is ready. Isn’t it simple?

Understanding each line of this program is very necessary if you want to learn Java.

Before running this program here is a complete explanation guide of your first program.

Though very concise and simple, we need to notice a few things:

Java Program Basic Questions

The following basic questions are very important if you want to excel in Java programming. If you are preparing for an interview, you must know.

Can we write a Java program without Class?

No!

First of all, every Java program is going to have at least one class.

The class that contains the main function is often referred to as the main class. We always declare the main function inside a class.

How can we write a Java program without a function?

However, you can write a Java program without any function which is contrary to many of other programming languages like C and C++.

class Main
{
    static
    {
        System.out.println("Hello, World!");
    }
}

Why do we write public static void main in Java?

Unlike C and C++, we do not simply declare void main() or int main(). We make the function public as well as static.

Note that we will compile and run our program on the command line. The interpreter needs access to the main function to run it. Hence, the main function has to be public.

We make the function static so that we can directly call the function on the command line with the class name without creating its object.

Why is string args used in Java?

String args [] is used to pass string arguments to the main function.

Here the arguments are blank []. However, they can be used to pass strings which are helpful due to various reasons which we will discuss after creating strong foundations for ourselves in Java.

What is the meaning of System.out.println()?

As we have already discussed, System is a class in Java’s java.lang package which is the main (root) package in Java.out is a static member of System class and represents the output buffer.

  • System is a class in Java’s java.lang package which is the main (root) package in Java.
  • Here, out is a static member of System class and represents the output buffer.
  • The keyword println is an overloaded function used for printing the output.

This line will always be used to print anything in Java.

Comment in Java

Commands are very important for any programming language. It gives readability to your program so that other people can understand your code in a human-readable format.

In Java, we can put single lines as well as multiline comments.

  • Single line comment use //
  • Multiline comments come between /*…..*/.

Compile and Run Hello World Program

Let us see how to compile and run the program on Windows.

I consider you have saved classpath and path variables for Java as I described in the JDK installation guide.

Now, go to the command line and change the current directory to the directory where the program is saved.

To compile the program, type:

javac Main.java

This step will create a ‘.class’ file wherever your program is saved. This .class file can be run on any platform.

To run the program, type in the command prompt:

java Main

Output:

You will see the output on your screen as below.

Hello, World!

So here we are done with the creation of our first Java program.

Let us create another small program where we have a look at the use of variables at the very basic level.

Exercise: Write a simple Java program to add two numbers.

class Main
{
    public static void main (String args [])
    {
        int a=20;
        int b=10;
        int c;
        c=a+b;
        System.out.println("The sum is "+c); 
        //Concatenation operator
    }
}

Just run this program to see the output. If you are not aware of integer variables, you don’t need to worry. I am going to describe it in the next sections of this tutorial.

Any doubt running your first program or related to Java program basic? Start the discussion in a comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *