In the previous post, we looked at how to install Java in your system and getting started with the first Java program. We looked at a very basic program in all of the computer languages that are to print HelloWorld. For most of the beginners, it is really tough to understand what each line of code actually does and hence we will go line by line through our HelloWorld program explaining the purpose of each line.
Going through code
For your reference I have added the code as below:
class HelloWorld { public static void main(String [] args){ System.out.println("Hello World"); } }
Let’s go through each line of code to understand Java in better terms!
Line 1 :
class HelloWorld{
The very first line in your Java code should either be the keyword ‘class’ followed by the name of the class or import statements, let’s keep the import statements for later and focus on just the name of the class for now. The first line now should be written as ‘class <class_name>’, here the word ‘class’ is a keyword, and ‘<class_name>’ is the name of the class which can be chosen accordingly and must begin with a capital letter. In this program, the <class_name> is ‘HelloWorld’ and all the contents of the class must be inside curly [‘{‘ and ‘}’] brackets .
Line 2 :
public static void main(String [] args){
This line is the main method of our Java program and is the entry point of our program, i.e. the program execution starts from this block of code. Every program must have a class that must have a main method like shown.
In the above line of code, we have the following –
- public – This is an access modifier. An access modifier can either be the default, public, private, or protected, we will learn more about access modifiers later. Here the access modifier is public so that runtime can execute this method.
- static – The main method must always be static so that JVM (Java Virtual Machine) can load the class into memory and call the main method.
- void – The main method doesn’t return any value, hence it is void. We will read more about the functions and their return types later.
- main – This is the name of the method and cannot be changed in any java program also no other method can be named as ‘main’ except the main method.
- String[] args – This is the argument that the main method accepts and is of the type String array.
This line of code is called the main method declaration and all contents of it must be enclosed within curly [‘{‘ and ‘}’] brackets .
Line 3 :
System.out.println("Hello World");
This line is used to print the arguments passed using the statement ‘System.out.println(args);’. In this program, we have passed the argument “Hello World” to print it using the above statement. Every statement must be terminated with a semi-colon ‘;’. Any argument can be passed to statement to print it.
Line 4 & Line 5 :
}
The line 4 and the line 5 contains two closing curly brackets which are the part of the main method and the class name respectively as stated above.
In next few posts we will discuss about classes, methods (functions) and Scanner class, so please stay tuned for upcoming post and in case of any doubts drop them below :)