Wednesday, 23 August 2017

Control Flow in the Hello World! Program

The actual flow of Hello World program.

In the previous post we discussed about how a java file is compiled and executed.
Now let us see the actual flow of the program with explaining used keywords in the program.


So coming to the first line of the as I told we define a class using the ‘class’ keyword. The class keyword defines the class name for the program or it is to say that all the lines of code below are named under a single class name or to say that these lines of program are categorized under some category.
Now consider the third line of the program which is called as main method declaration. Execution of every program starts from the main method.
In main method the word public means that the code under main is accessible by every member of the class(of course) and also the members of other classes. The word public is called as an access-specifier. For now just remember the above word as access-specifier(namely public, private and protected) will be dealt in detail in further upcoming posts.
static : Remember that in java to access a member of a function or a method we need to create an object of that class and we access the members. This is also called as object messaging which is also an important achievement of Object Oriented Programming.
To understand the use of static keyword consider the below program.



 



The program ran successfully! (Well didn’t expect that!)
As said earlier for accessing a member of a method we need to create an object(also called as an instance) of the class. But I don’t see any object creation there. How did the program run?
The answer is the keyword ‘static’ we used in the program allows the main() to be called without creating an object of the class StaticExample. If its confusing don’t worry we will still discuss these keywords in the upcoming posts along with examples.
(String[] args) are the arguments to the function main.
The line “System.out.println(“The value of a is:”+a); prints the value of a along with the given String given as a parameter to println function.


    Meaning of “System.out.println()” statement:


As mentioned above the built-in println() method displays the strings that are passed to it and also other type of data passed.
The keyword “System” is a predefined class in the java Library that provides access to the system to get different types of input and output streams and “out” is the output stream connected to the console/terminal.
The different types of streams will be discussed in detail in further posts.
That completes the explanation of the Introductory programs for java.



In next session we will deal in writing simple java programs first and then move onto Object creation topic.
 
BYE! And keep visiting


 

No comments:

Post a Comment

             The `this` keyword in java The this keyword in java can be used for different purposes. Some of these are listed below. ...