Page 47 - Introduction to Programming with Java: A Problem Solving Approach
P. 47
1.6 Portability 13 wise, if you have object code that was created on a type Y computer, then that object code can run only on
3
ufacturers care. If they want to sell a program that runs on different computer types, they typically have to compile their program on the different computer types. That produces different object-code files, and they then sell those files. Wouldn’t it be easier if software manufacturers could provide one form of their program that runs on all types of computers?
Java’s Solution to the Portability Problem
The inventors of Java attempted to address the inherent lack of portability in object code by introducing the bytecode level between the source code and object code levels. Java compilers don’t compile all the way down to object code. Instead, they compile down to bytecode, which possesses the best features of both object code and source code:
• Like object code, bytecode uses a format that works closely with computer hardware, so it runs fast. • Like source code, bytecode is generic, so it can be run on any type of computer.
How can bytecode be run on any type of computer? As a Java program’s bytecode runs, the bytecode is translated into object code by the computer’s bytecode interpreter program. The bytecode interpreter program is known as the Java Virtual Machine, or JVM for short. Figure 1.5 shows how the JVM translates bytecode to object code. It also shows how a Java compiler translates source code to bytecode.
Apago PDF Enhancer
source code
bytecode
object code
Figure 1.5 How a Java program is converted from source code to object code
To run Java bytecode, a computer must have a JVM installed on it. Fortunately, installing a JVM is straightforward. It’s a small program, so it doesn’t take up much space in memory. And it’s easy to obtain— anyone can download a JVM for free from the Internet. In Section 1.8, we explain how to download a JVM and install it on your own computer.
3 There are about 15 or so different computer types that are in common use today. Those 15 computer types correspond to 15 catego- ries of CPUs. Each CPU category has its own distinct instruction set. An instruction set defines the format and meanings of all the object-code instructions that work on a particular type of CPU. A full discussion of instruction sets is beyond the scope of this book. If you’d like to learn more, see Wikipedia’s Web site at http://en.wikipedia.org/ and enter “instruction set” in the search box.
a type Y computer.
So what’s all the fuss about portability? Who cares that object code is not very portable? Software man-
Java compilers perform this translation as part of the compilation process
The JVM performs this translation as part of the run process