Before going
into more details of Java, it is important to understand core of Java. Let’s
dive into concepts like JVM, JIT, JDK, JRE.
JVM (Java Virtual Machine)
The name JVM
explains itself. It is a virtual machine
that executes Java code. As it’s a virtual machine it can be installed on
any other physical machine.
JVM is responsible
for allocating and deallocating memory space to program.
Do
you know
|
JVM is written in C language. It is operating system dependent.
|
How JVM works
1.
Compiler
converts java source code into bytecode.
2.
Byte
code is then loaded into JVM.
3. In a JVM,
each and every class is loaded by some instance of a java.lang.ClassLoader.
4.
Then
bytecode verifier traverses the bytecode, constructs the type state
information, and verifies the types of the parameters to all the bytecode
instructions. The bytecode verifier acts as a sort of gatekeeper: it ensures
that code passed to the Java interpreter is in a fit state to be executed and
can run without fear of breaking the Java interpreter.
5.
JIT
interprets parts of bytecode and try optimizing code performance by replacing
original instructions by similar but efficient native instructions.
6.
Once
JIT optimized code, interpreter reads bytecode and converts it into machine
instructions and sends to operating system.
7.
OS
kernel then executes instruction on underlined hardware.
Do you know:
why Java called as platform independent language?
|
Before going into the detail, first you have
to understand what is the mean of platform? Platform consists of the computer
hardware (mainly architecture of the microprocessor) and OS.
Platform=hardware +Operating System.
Anything that is platform independent can run
on any operating system and hardware.
Java is platform independent so java can run
on any operating system and hardware. Now question is how it is platform
independent?
This is because of the magic of Byte Code
which is OS independent. When java compiler compile any code then it generate
the byte code not the machine native code (unlike C compiler).Now this byte
code need a interpreter to execute on a machine. This interpreter is JVM. So
JVM read that byte code (that is machine independent) and execute it.
Different JVM is designed for different OS and byte code is able to run on
different OS.
In case of C or C++(language that are not
platform independent) compiler generate the .exe file that is OS dependent so
when we run this .exe file on another OS it will not run because this file is
OS dependent so is not compatible with the another OS.
Finally, an intermediate OS independent byte
code makes the java platform independent.
|
No comments:
Post a Comment