Sunday, 29 January 2017

Overview of Java Language Introduction

Introduction
Java is a general purpose object oriented programming language. We can write two type of program in java: Stand alone application and Web applets.

Stand alone application: Stand alone applications are the programs written in java to perform certain task on local computer. Stand alone java applications can read and write files and perform some other tasks which applets can’t do. 

Web applets: web applets are the java programs developed for internet applications. Web applets works on web pages. An applet located at remote computer (server) can be downloaded using internet and executed on local computer (client). Java applets can be developed for any kind of application from simple animated graphics to complex games and utility.  An applet can run only within a browser.

Simple Java Program (Hello World Application)
We begin with a simple hello world java program that prints a line “Hello World!” as an output.
1.      Start Eclipse
2.      Open new java project – go to menu option:
File -> New -> Java Project
A window “New Java Project” is opened. Give your project name in “Project name” option in this window. For example we give project name as “MyFirstJavaProject”.
3.       

_____________________________________________________________________
1.  public class FirstJavaProgram
2.  {
3.     public static void main(String[] args)
4.     {
5.          System.out.println("Hello World!");
6.     }
7.      }
______________________________________________________________________
Simple Java Program

This simple java program brings some simple java language salient features. Let us discuss them step by step (Line by line) and understand unique features of java program.

1.      Class Declaration
The first line:  public class FirstJavaProgram

Declares a class which is an object oriented construct. As we have already discussed that java is an object oriented language so everything in a java program must be placed inside a Class. The class word in this line is a key word which is used to define a class. FirstJavaProgram is the java identifier of the class which specifies the name of the class. We can give any name to the class as per our choice or requirement. The first word public in this line specifies that class can be accessed from any other class.

2.      Opening Brace

Second and last line: “{“and “}

Every class in java begins with opening brace “{” as we can see in the second line of the example and ends with closing brace “}” as we can see in the last line of the example.

3.      The Main Line

Third line:  public static void main(String[] args)

This line defines a method with name main. This is similar to the main () method of the C or C++.  Every java application must have a main method. This is also called entry point of the program. Interpreter begins execution of the program from this point. Java application can have many classes but only one class must have main method. There is no main method in java applets.
This line contains three keywords public, static and void.
Public:  The public is an specifies the access of the main method as unprotected and making it accessible to all other classes. 
Static: It declares main method is one that belongs to the entire class not a part of any object of the class. Main must be always declared as static because it is the entry point of the program and interpreter use it before creation of any object.    
Void: It specifies that main method doesn’t return any value.
            The main method of this line also contains a parameter (String[] args).
       String[]: It is class which contains an array of type string.   
Args: it is parameter passed in the main method, it contains an array of objects the class type String.
4.      Body of the main method

Line 4 and 6: Starting brace “{” and ending brace “}”
Every method in java begins with “{” which is starting point of the body of a method. A method may have many lines of the code. Similarly every method of the java ends with “}”.  The code between these braces “{…}” is called as body of the method.

5.      The Output Line

Line 5:       System.out.println("Hello World!");

When we run this program this statement will display a line “Hello World!” on the screen which is an output of this program. This is similar to the printf() statement of the C language and cout<< statement of the C++ language. This is only executable statement in this small program. As java is a true object oriented language, every method must be the part of some object.

The println method is a member of out object.
Out object is static data member of class System.
System is a standard class of java.
The method println() always appends a new line character at the end of every println statement. It means every subsequent output will start at new line on the output screen. There is a semicolon at the end of this line; it means every statement in java code must end with a semicolon.

Comments
Comments are the text in the code which does not show up in the code. Editor and compilers ignores commented lines in the program and doesn’t consider them as the statement of java. We can add as many as comments in the code as we need. There are three ways in java to make comments. The common method is double front slash “//”. The comment will start from // and up to the end of the line. We can use /* and */ to comment a block instead of using // for each and every line. Third type of comment is /** and */.
Example
1. /**
2. * This is first java program
3. * It is hello world program
4. * @version 1.1
5. * @author HS
6. *
7. */
8. public class FirstJavaProgram
9.     {
10.           /*
11.            The name of class is FirstJaveProgram
12.            main is the entry point of the java program
13.            main has an argument
14.            */
15.           public static void main(String[] args)
16.           {
17.            System.out.println("Hello World!"); // Display out put
18            //     No more output statements
19.           }
20.   }
Commented lines in above program:
Line no. 17 and 18:  Example of single line comment “//”.
Line no 10 to 14: Example of block comment “/*….*/”

Line no 1 to 7: Automatic documentation comment “/**….*/”

No comments: