Programming and Problem Solving with Java

(やまだぃちぅ) #1
1.2 How Is Java Code Converted into a Form That a Computer Can Use? | 13

SOURCE CODE
(C++)

COMPUTER
EXECUTES
TRANSLATOR
CODE
(COMPILER)

OBJECT
CODE
(MACHINE
LANGUAGE
VERSION OF
SOURCE CODE)

COMPUTER
EXECUTES
OBJECT CODE

Windows PC
C++ compiler

UNIX
workstation
C++ compiler

C++ Code

Macintosh
C++ compiler

Windows PC
machine
language

UNIX
workstation
machine
language

Macintosh
machine
language

Windows PC
computer

UNIX
workstation
computer

Macintosh
computer

Figure 1.5 High-Level Programming Languages Allow Applications to Be Compiled on Different Systems


No computers actually use Bytecode as their machine language. Instead, for a computer
to run Bytecode, it must have another program called the Java Virtual Machine (JVM) that
serves as a language interpreter for the Bytecode (we will explain the meaning of the term
“virtual machine” shortly). Just as an interpreter of human languages listens to words spo-
ken in one language and then speaks a translation of them in a language that another per-
son understands, so the JVM reads the Bytecode instructions and translates them into
machine language operations that the particular computer executes. Interpretation takes
place as the Bytecode is running, one instruction at a time. This process is not the same as
compilation, which is a separate step that translates all of the source code instructions in a
program prior to execution. Figure 1.6 shows how the Java translation process achieves
greater portability.
As Figure 1.6 illustrates, the compiled Java code can run on any computer that has the
JVM program available to interpret for it, which means that the Java application does not have
to be compiled for each type of computer. This level of portability has grown in importance
as computers across the globe are connected by the World Wide Web (also called the Web: a
part of the Internet). For example, a programmer can write a Java application and make its
Bytecode available to the public via the Web without having to recompile it for the many dif-
ferent types of computers that may be used to run it.

Free download pdf