Monday, 29 August 2011

Java program compilation process

Dynamic compilation-- a brief history

                 The compilation process of Java is a little different from that of statically compiled languages like C/C++. A static compiler converts source code directly to machine code that can be directly executed on the target platform.


                 The Java compiler converts Java source into JVM bytecodes which are "virtual machine instructions" for the JVM.



Bytecodes-->   Bytecode is the computer object code that is processed by a program usually referred to as "virtual machine" rather than by the real computer machine, the hardware processor. A bytecode program may be executed by parsing and directly executing the instructions, one at a time. This kind of bytecode interpreter is very portable. 

Java Virtual Machine--> A Java Virtual Machine is a virtual machine capable of executing Java bytecode. JVMs are available for many hardware and software platforms. The use of the same bytecode for all JVMs on all platforms allows Java to be described as a "compile once, run anywhere" programming language, as opposed to "write once, compile anywhere", which describes cross-platform compiled languages. Thus, the JVM is a crucial component of the Java platform.




The creation of a Java program generally follows the steps below.





The code is developed in either a plain text editor or an Integrated  Development   Environment (IDE) and saved to a file with a .java extension. It is then checked for syntax errors by the Java Compiler (javac.exe). If successfully compiled, the resultant .class file is passed to the Java Virtual Machine (java.exe) for execution.
The act of compiling has a two-fold effect: Firstly, the code is checked for syntax errors and secondly, the code is converted to byte codes. The byte codes are instructions for an imaginary machine called the Java Virtual Machine (JVM). This machine is emulated by all Java interpreters and therefore allows you to execute a compiled Java program among different platforms (operating systems with a JVM).

      Java bytecode is an intermediate language which is typically compiled from Java, but it can also be compiled from other programming languages. For example, Ada source code can be compiled to Java bytecode and executed on a JVM.


Now, you can just stop here and move on to the next topic/tutorial because what i am going to discuss next might be a little difficult to understand to you. It is not an essential topic, but you can certainly increase your knowledge if you read it.




The details of Bytecode & JVM-->


       To understand the details of bytecode, we need to discuss how JVM works regarding the execution of the bytecode. A JVM is a stack-based machine. Each thread has a JVM stack which stores frames. A frame is created each time a method is invoked, and consists of an operand stack, an array of local variables, and a reference to the runtime constant pool of the class of the current method. Conceptually, it might look like this: 



Figure 1. A frame
frame 



The array of local variables, also called the local variable table, contains the parameters of the method and is also used to hold the values of the local variables. The parameters are stored first, beginning at index 0. If the frame is for a constructor or an instance method, the reference is stored at location 0. Then location 1 contains the first formal parameter, location 2 the second, and so on. For a static method, the first formal method parameter is stored in location 0, the second in location 1, and so on.
The size of the array of local variables is determined at compile time and is dependent on the number and size of local variables and formal method parameters. The operand stack is a LIFO stack used to push and pop values. Its size is also determined at compile time. Certain opcode instructions push values onto the operand stack; others take operands from the stack, manipulate them, and push the result. The operand stack is also used to receive return values from methods.



Next we shall be looking to start preparing for our first Java program.



>>Next Tutorial>>      
<<Previous Tutorial<<

_____________________________________________________________________________________

No comments:

Post a Comment