Java incorporates elements of both interpretation and compilation.
Here is more information on these two approaches:
Interpretation
An interpreter reads in one line of a program and executes it
before going to the next line. The line is first parsed to its smallest
component operations and then each operation executed. (This could
be done with something like the switch
statement in C with every possible operation case listed.)
The interpreter is normally a program compiled into the local machine
code so its operations run at full speed.
BASIC was one of the earliest interpreted languages and each text
line is interpreted. Similarly, scripting languages like PERL are
also interpreted.
Interpretation simplifies the programming environment since there
are no intermediate steps between writing or modifying the code
and running it. Results are known immediately, so debugging is fast.
Also, the programs are easily transportable to other platforms (if
an interpreter is available on them.)
The drawback is slow performance. The interpreter must read a line,
translate it and find the corresponding machine level code, and
then execute the instructions.
Compilation
The program text file is first converted to native machine code
with a program called a compiler. (A linker program
may also be necessary to connect together multiple program code
files.) The output file of the compiler is the executable program
that runs. Fortran, C/C++, and Pascal are all compiled languages.
The biggest advantage of compiled language is the fast performance
since the machine code instructions load directly into the processor
and execute.
In addition, the compiler can optimize the program since it looks
at the whole program at once rather than simply line by line as
with the interpreter.
The disadvantages include slower debugging since after every correction
and modification, the program must be recompiled.
Also, since the executable is in local machine code, the executable
files are not usually transportable to other platforms. The source
code must be recompiled on those machines.
The Java Approach
Java incorporates both interpretation and compilation. The
text program is compiled to the "machine" code, called
bytecodes, for the Java Virtual Machine (JVM or just VM).
The JVM simulates a processor that executes the bytecode instructions.
(In Chapter 23 we discuss hardware
processors that directly execute Java bytecodes.)
The JVM interprets the bytecodes. The bytecodes can be run on any
platform on which a JVM has been developed. The program runs "inside"
the JVM so it doesn't care what platform it is on.
Thus, Java attempts to get the best of both worlds. The compilation
step allows for some degree of optimization of the code and the
JVM interpretation allows for portability.
There remains the drawback of an extra compilation step after every
correction during debugging. Also, the interpretation of bytecodes
is still slower in many cases than a program in local machine code.
Advanced JVM's can ameliorate this, however,
and in many cases now reach speeds similar to programs compiled
to local machine code.
Latest update: Dec.10.2003
|