Print "Hello World" - Our first JAVA Program

In this tutorial, we will work on our first JAVA program and have a detailed explanation of each part of it.

Print "Hello World" - Our first JAVA Program

Now its time to have a look at our first JAVA program. We have already talked about some of the basic concepts of JAVA and now we will work on actual programs and along with theory related to the program and topics left to cover.

FIRST JAVA PROGRAM:

Now we will take up a simple example of a JAVA program that prints a string on the screen. We will print Hello World as a string in this print. So let's get started with a program to print Hello World in JAVA.

Program to print a string on the screen:

// My first JAVA program
public class FirstJavaProgram {
       public static void main(String args[])  {
           System.out.println("Hello World");   // prints Hello World
     }
}

The above program is one of the simplest JAVA programs, It includes basic elements that every JAVA program has. Let's break down each part of this program and learn something about it.

// My first JAVA program:

This is a comment. A line beginning with // are comments in JAVA. The compiler does not execute comments. Comments are included only for the description of our code and to make our code easy to understand. There are two types of comments: 

  • Single Line comments
  • Multi Line comments

Public class FirstJavaProgram : 

This statement is mostly the very first line of our program which has three components listed in it:]

  1.  Public : It is an access modifier which can be assigned to variables, methods, constructors, and classes. It is the most non-restricted type of access modifier. The public access modifier is accessible everywhere. So, we can easily access the public inside and outside the class and package.
  2. class : Java is an object-oriented programming language therefore everything in java is associated with classes and objects. A class is a blueprint prototype of an object.
  3. FirstJavaProgram : Name given to the class.

public static void main(String[] args) :

Understanding this statement in brief by dividing it into subparts :

  1. public : It is an access modifier which can be assigned to variables, methods, constructors, and classes. It is the most non-restricted type of access modifier. The public access modifier is accessible everywhere. So, we can easily access the public inside and outside the class and package.
  2. static : It is a keyword which is  associated to a method to make it a class related method.  
  3. void : It is a keyword which is used to specify that the method or the function does not return any value.
  4. main : It is the name of JAVA main method. It is not a keyword.
  5. String[] args : It stores Java command line arguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and user can use any name in place of it.

System.out.println("Hello World") :

In JAVA , System.out.println is used to print any argument passed into it. The statement can be boken down into subparts to have a greater understanding.

  1. System : It is a final class defined in java.lang package.
  2. out : It is an instance of PrintStream type which is a public and static member field of the System class.
  3. println() : It is a public method of the PrintStream class. Here 'ln' signifies each time print the output in a new line.
  4. parameter : Anything that user wishes to display on the screen.

We will look more about it in future tutorials.

Congratulation on writing the first JAVA program we wish you the best of luck for your journey to learn JAVA with us. We will be covering more topics in other tutorials so for now, this is the end of this tutorial we will learn more in the next JAVA tutorial. 

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0