Pages

Programming Structure,JAVA



Comments: Comments are description about the aim and features of the program. Comments increase readability of a program. Three types of comments are there in Java:
· Single line comments: These comments start with //
e.g.:  // this is comment line
· Multi line comments: These comments start with /* and end with */
e.g.: /* this is comment line*/
· Java documentation comments: These comments start with /** and end with */
These comments are useful to create a HTML file called API (application programming Interface) document. This file contains description of all the features of software.

Structure of the Java Program:
· As all other programming languages, Java also has a structure. 
· The first line of the C/C++ program contains include statement. For example, <stdio.h> is the header file that contains functions, like printf (), scanf () etc. So if we want to use any of these functions, we should include this header file in C/ C++ program. 
·  Similarly in Java first we need to import the required packages. By default java.lang.* is imported. Java has several such packages in its library. A package is a kind of directory that contains a group of related classes and interfaces. A class or interface contains methods.
· Since Java is purely an Object Oriented Programming language, we cannot write a Java program without having at least one class or object. So, it is mandatory to write a class in Java program. We should use class keyword for this purpose and then write class name.
· In C/C++, program starts executing from main method similarly in Java, program starts executing from main method. The return type of main method is void because program starts executing from main method and it returns nothing.
Sample Program:
//A Simple Java Program  import java.lang.System; import java.lang.String;
class Sample
{
            public static void main(String args[])
                        {
                                    System.out.print ("Hello world");
                        }
}
· Since Java is purely an Object Oriented Programming language, without creating an object to a class it is not possible to access methods and members of a class. But main method is also a method inside a class, since program execution starts from main method we need to call main method without creating an object.
· Static methods are the methods, which can be called and executed without creating objects. Since we want to call main () method without using an object, we should declare main () method as static. JVM calls main () method using its Classname.main () at the time of running the program.
· JVM is a program written by Java Soft people (Java development team) and main () is the method written by us. Since, main () method should be available to the JVM, it should be declared as public. If we don’t declare main () method as public, then it doesn’t make itself available to JVM and JVM cannot execute it.
· JVM always looks for main () method with String type array as parameter otherwise JVM cannot recognize the main () method, so we must provide String type array as parameter to main () method.
· A class code starts with a {and ends with a}. A class or an object contains variables and methods (functions). We can create any number of variables and methods inside the class. This is our first program, so we had written only one method called main (). 
· Our aim of writing this program is just to display a string “Hello world”. In Java, print () method is used to display something on the monitor. 
· A method should be called by using objectname.methodname (). So, to call print () method, create an object to PrintStream class then call objectname.print () method. 
· An alternative is given to create an object to PrintStream Class i.e. System.out. Here, System is the class name and out is a static variable in System class. out is called a field in System class. When we call this field a PrintStream class object will be created internally. So, we can call print() method as:  System.out.print (“Hello world”);
· println () is also a method belonging to PrintStream class. It throws the cursor to the next line after displaying the result.
· In the above Sample program System and String are the classes present in java.lang package.

Escape Sequence: Java supports all escape sequence which is supported by C/ C++. A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly.
Escape Sequence
Description
\t
Insert a tab in the text at this point. 
\b
Insert a backspace in the text at this point. 
\n
Insert a newline in the text at this point. 
\r
Insert a carriage return in the text at this point. 
\f
Insert a form feed in the text at this point. 
\'
Insert a single quote character in the text at this point. 
\"
Insert a double quote character in the text at this point. 
\\
Insert a backslash character in the text at this point.

Creating a Source File:
· Type the program in a text editor (i.e. Notepad, WordPad, Microsoft Word or Edit Plus). We can launch the Notepad editor from the Start menu by selecting Programs > Accessories > Notepad. In a new document, type the above code (i.e. Sample Program). 
· Save the program with filename same as Class_name (i.e. Sample.java) in which main method is written. To do this in Notepad, first choose the File > Save menu item. Then, in the Save dialog box:
o   Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is JQR on the D drive. 
o   In the File name text field, type "Sample.java", including the quotation marks. Then the dialog box should look like this:
Compiling the Source File into a .class File: 
· To Compile the Sample.java program go to DOS prompt. We can do this from the Start menu by choosing Run... and then entering cmd. The window should look similar to the following figure


· The prompt shows current directory. To compile Sample.java source file, change current directory to the directory where Sample.java file is located. For example, if source directory is JQR on the D drive, type the following commands at the prompt and press Enter:  


   Now the prompt should change to D:\JQR>
· At the prompt, type the following command and press Enter.  javac Sample.java 
· The compiler generates byte code and Sample.class will be created.

Executing the Program (Sample.class):
· To run the program, enter java followed by the class name created at the time of compilation at the command prompt in the same directory as: java Sample 

· The program interpreted and the output is displayed.

The Java Virtual Machine: Java Virtual Machine (JVM) is the heart of entire Java program execution process. First of all, the .java program is converted into a .class file consisting of byte code instructions by the java compiler at the time of compilation. Remember, this java compiler is outside the JVM. This .class file is given to the JVM. Following figure shows the architecture of Java Virtual Machine

Figure:  The internal architecture of the Java virtual machine.
In JVM, there is a module (or program) called class loader sub system,which performs the following instructions:
· First of all, it loads the .class file into memory. 
· Then it verifies whether all byte code instructions are proper or not. If it finds any instruction suspicious, the execution is rejected immediately.
· If the byte instructions are proper, then it allocates necessary memory to execute the program. This memory is divided into 5 parts, called run time data areas, which contain the data and results while running the program. These areas are as follows:
o   Method area: Method area is the memory block, which stores the class code, code of the variables and code of the methods in the Java program. (Method means functions written in a class).
o   Heap: This is the area where objects are created. Whenever JVM loads a class, method and heap areas are immediately created in it.
o   Java Stacks: Method code is stored on Method area. But while running a method, it needs some more memory to store the data and results. This memory is allotted on Java Stacks. So, Java Stacks are memory area where Java methods are executed. While executing methods, a separate frame will be created in the Java Stack, where the method is executed. JVM uses a separate thread (or process) to execute each method.
o   PC (Program Counter) registers: These are the registers (memory areas), which contain memory address of the instructions of the methods. If there are 3 methods, 3 PC registers will be used to track the instruction of the methods.
o   Native Method Stacks: Java methods are executed on Java Stacks. Similarly, native methods (for example C/C++ functions) are executed on Native method stacks.  To execute the native methods, generally native method libraries (for example C/C++ header files) are required. These header files are located and connected to JVM by a program, called Native method interface.
Execution Engine contains interpreter and JIT compiler which translates the byte code instructions into machine language which are executed by the microprocessor. Hot spot (loops/iterations) is the area in .class file i.e. executed by JIT compiler. JVM will identify the Hot spots in the .class files and it will give it to JIT compiler where the normal instructions and statements of Java program are executed by the Java interpreter.