Understanding Java Programs
You have already seen the making and executing of a Java program. Understanding a program is the most important task. Have a closer look at “first.java”
Line 1::
/* Multiline Comment*/ The comments on program are enclosed within it. The compiler ignores these lines and hence no error is generated. Writing comments is not binding on the user.
Line 2::
class first This is the second line of our java program. Here we have created a class named “first”. Creating a class is binding on the programmer as Java is strictly object oriented. Everything is encapsulated within a class.
Line 3::
{ Opening braces Braces play a vital role in programming. A group of statements are enclosed in braces called blocks. Brace, here depicts that the class has begin.
Line 4::
public static void main(String args[]) In this line we have created a method or function using a list of java “keywords”. A keyword is a special word that is recognized by the compiler and has a special use.
Keywords
- public -- An access specifier, it declares that this method can be accessed by members who are even outside this class.
- static -- We have created a static method named “main”. In the context of methods “static” keyword means that this method is independent of any object. As main is executed before any other method, we need to make it static so that we can call other methods by creating their instances here.
Special Note—You will notice that when the same program was written in BlueJ, we did not mention the keyword “static”. So at the time of executing we had to create an object. When we are not working with BlueJ we have to manually create an object. The format is --
Class name objectname=new classname(); But as here we have a static method we don’t need to create any hd
- void --You will learn in the upcoming chapters that a method can even return a value. The “void” keyword here means that the method does not return any value. If these things seem too complicated to you, have patience!! Soon each and every detail will be clear.
- main (String args[]) -- We have now specified the main method. The brackets enclose within themselves ‘the parameters’. Here we have an array of instances of String class. (Arrays are a group of one kind of data and String is a predefined class in Java that stores series of characters in itself).
Line 5:: { Opening Brace
By this brace we come to know that our method has begun. It defines the starting point of method.
Line 6::
System.out.println(“This is a java program”); This is an output statement. Well, there is good reason for this line working like magic and sincerely you don’t need to mug it up. In Java we take input and output in forms of “Streams”. Imagine it as a flow of data. System.out.println(); directs all the contents within the brackets as “Output Streams” to the console. Here System is a predefined class, out an object and println() a method. Once you have studied in detail about classes and objects, you will understand this scary business much better. // Printing a statement This is another way of writing comments. The difference being that this is a single line comment. It has a scope till the end of line. For commenting on the next line, we have to again insert //.
Remember — Writing comments are not binding on the programmer but it is a good practice to insert comments in your code. It makes the code clean, readable and understandable.
Line 7:
} Closing Braces These braces tell us that the method has ended. Now all other statements will not be considered as a part of that method.
Line 8:: }
These braces illustrate the ending of class.