Java Reference
In-Depth Information
While some versions of Java do translate your program into machine language for
your particular computer, the original Java compiler and most compilers today do
not. Instead, the Java compiler translates your Java program into a language called
byte-code . Byte-code is not the machine language for any particular computer; it is the
machine language for a fictitious computer called the Java Virtual Machine (JVM) .
The Java Virtual Machine is very similar to all typical computers. Thus, it is easy to
translate a program written in byte-code into a program in the machine language
for any particular computer. The term JVM is also used to refer to the software
that implements the fictitious computer. There are two ways the JVM can do this
translation: through an interpreter and through a Just-In-Time (JIT) compiler.
An interpreter combines the translation of the byte-code and the execution of the
corresponding machine language instructions. The interpreter works by translating an
instruction of byte-code into instructions expressed in your computer's machine language
and then executing those instructions on your computer. It does this one byte-code
instruction at a time. Thus, an interpreter translates and executes the instructions in the
byte-code one after the other, rather than translating the entire byte-code program at once.
Modern implementations of the JVM use a JIT compiler, which uses a combination
of interpretation and compilation. The JIT compiler reads the byte-code in chunks
and compiles entire chunks to native machine language instructions as needed. The
compiled machine language instructions are remembered—i.e., cached—for future
use, so the chunk needs to be compiled only once. This model generally runs programs
faster than the interpreted model, which always has to translate the next byte-code
instruction to machine code instructions.
To run a Java program, first use the compiler to translate the Java program into
byte-code. Then, use the JVM for your computer to translate byte-code instructions to
machine language and to run the machine language instructions.
It sounds as though Java byte-code just adds an extra step in the process. Why not write
compilers that translate directly from Java to the machine language for your particular
computer? This is what is done for most other programming languages. However, Java
byte-code makes your Java program very portable. After you compile your Java program
into byte-code, you can use that byte-code on any computer. When you run your program
on another type of computer, you do not need to recompile it. This means that you can
send your byte-code over the Internet to another computer and have it easily run on that
computer. This is one of the reasons Java is good for Internet applications. This model is
also more secure. If a Java program behaves badly, it only does so within the context of the
JVM instead of behaving badly directly on your native machine. Of course, every kind of
computer must have its own program to implement the Java Virtual Machine.
byte-code
Java Virtual
Machine
(JVM)
Interpreter
Just-In-Time
(JIT)
Byte-Code
The Java compiler translates your Java program into a language called byte-code , which is
the machine language for a fictitious computer. It is easy to translate this byte-code into the
machine language of any particular computer. Each type of computer will have its own software
to implement the Java Virtual Machine that translates and executes byte-code instructions.
 
Search WWH ::




Custom Search