(^14) | Introduction to Object-Oriented Programming
Java program Java compiler Java Bytecode
Windows PC
running JVM
UNIX workstation
running JVM
Macintosh
running JVM
Figure 1.6 A Java Compiler Produces Bytecode that Can Be Run on Any Machine with the JVM
1.3 How Does Interpreting Code Differ from Executing It?
Direct executionof code differs significantly from interpretationof code. A computer can directly
execute a program that is compiled into machine language. The JVM, for example, is a ma-
chine language program that is directly executed. The computer cannot directly
execute Bytecode, however. Instead, it must execute the JVM to interpret each
Bytecode instruction so as to run the compiled Java source code. The JVM does not
produce machine code, like a compiler, but rather it reads each Bytecode instruc-
tion and gives the computer a corresponding series of operations to perform.
Because each Bytecode instruction must first be interpreted, the computer can-
not run Bytecode as quickly as it can execute machine language. Slower execution
is the price we pay for increased portability.
1.4 How Is Compilation Related to Interpretation and
Execution?
It is important to understand that compilationand executionare two distinct
processes. During compilation, the computer runs the compiler. During execution, the object
code is loaded into the computer’s memory unit, replacing the compiler. The computer then
directly executes the object code, doing whatever it is instructed to do (see Figure 1.7).
We can use the JVM as an example of the process shown in Figure 1.7. The JVM is written
in a high-level programming language such as C++ and then compiled into machine language.
This machine language is loaded into the computer’s memory, and the JVM is executed. Its
input consists of Java Bytecode. Its results are the series of actions that would take place if the
computer could directly execute Bytecode. Figure 1.8 on page 16 illustrates this process.
In looking at Figure 1.8, it is important to understand that the output from the compil-
ers can be saved for future use. Once the JVM and the Java application have been compiled,
they can be used repeatedly without being recompiled. You never need to compile the JVM,
in fact, because that step has already been done for you. Figure 1.8 shows its compilation sim-
Direct execution The process
by which a computer performs
the actions specified in a
machine language program
Interpretation The
translation, while a program is
running, of nonmachine-
language instructions (such as
Bytecode) into executable oper-
ations