Java Virtual Machine

Java uses both Compiler and Interpreter. Source code written in Java is compiled to generate bytecode and then this bytecode is interpreted to machine instructions for specific machine.

The bytecode generated by the compiler is not machine specific. It is generated for the virtual machine that exists only the computer memory known as JAVA VIRTUAL MACHINE(JVM).This virtual machine is designed in such a way that it can be implemented on the top of any existing processor and itself acts as virtual processor chip. It hides the underlying operating system details from Java applications. The process of converting source code to machine code is shown in this figure:

 Java Virtual Machine


JVM obtains the bytecode stream from the .class file in the form of series of instruction. Each instruction consists of a one-byte opcode(valid and understandable command which tells the JVM what to do)and zero or more operands, which are required to complete the opcode.

Some of the main components of JVM are stack,registers, garbage-collection heap and method area.

1)Stack: JVM stacks are similar to the stack used in C or other conventional languages. It consists of frames, which holds the state of Java method invocation. It holds the local variables, parameters, its return value and the intermediate results. JVM stack cannot be manipulated directly and only pushing and popping of frames can takes place. When a method is invoked, the JVM pushes a new frame into the stack and after the completion of execution of the method the frame is popped and discarded by the virtual machine. The size of the JVM stack can be fixed or may dynamically increase and decrease according to the requirement.

2)Registers: JVM uses different types of registers to control the stacks. Some of the important JVM registers are:

           a)Optop:It points to the top of operand stack.

         b)Frame:It points to the current execution environment.

         C)Vars:It points to the local variables.

         d)Program Counter:It keeps record of the next instruction in a program to be executed.

3)Garbage-Collection Heap: Heap is a free memory space used for allocation to the variables like arrays, class instances, runtime. Whenever memory is allocated with the help of new operator, that memory space is allocated from the heap. Heap provides an automatics storage management system known as garbage-collection heap. Whenever there are objects which are no longer referenced, then the runtime environment automatically reclaims the memory occupied by the objects. Size of the heap may be fixed or may increase or decrease according to the requirement.

4)Method area:Method area is a storage area for the compiled code. Although the method area is a part of heap, there is no specific area where it exists. This feature makes JVM more portable and secure. Size of the method area may be fixed or may increase or decrease according to the requirement. Method is used to store:

    a)Runtime constant pool

   b)Field information

   c)Method information

   d)Code for methods and constructors

   e)Special methods that are used in class and instance initialization.

   f)Interface type initialization

The primitive data types that can be handled by the JVM are byte(8 bits), char(16 bits), short(16 bits),int(32 bits),float(32 bits),long(64 bits), double(64 bits).