Java Reference
In-Depth Information
Bytecode
JVM
on
Windows
JVM
on
UNIX
JVM
on
Mac
Windows
UNIX
Mac
Figure 2-18. A JVM as a translator between bytecode and an operating system
A Java program compiled into bytecode format has two advantages:
You do not need to recompile your source code if you want to run it on another machine with
a different operating system. It is also called platform independence in Java. It is also known as
“write once, run anywhere” for Java code.
If you are running a Java program over a network, the program runs faster because of the
compact size of the bytecode format, which results in less loading time over the network.
In order to run a Java program over the network, the size of the Java code must be compact enough to be
transported over the network faster. The class file, which is generated by a Java compiler in bytecode format, is very
compact. This is one of the advantages of compiling the Java source code in bytecode format.
The second important advantage of using bytecode format is that it is architecture-neutral. By bytecode format
being architecture-neutral, it means if you compile the Java source code on a specific host system, say, Windows, the
generated class file does not have any mention or effects that it was generated on Windows. If you compile the same
Java source code on two different host systems, for example, Windows and UNIX, both class files will be the same.
The class file in bytecode format cannot be directly executed on a host system because it does not have any host
system-specific direct instructions. In other words, we can say that bytecode is not a machine language for any specific
host system. Now, the question is who understands the bytecode and who translates it into underlying host system
specific language? The JVM performs this job. The bytecode is the machine language for the JVM. If you compile Java
source code to generate a class file on Windows, you can run the same class file on UNIX if you have a Java platform
(JVM and Java API collectively are known as the Java platform) available on the machine running on UNIX. You do
not need to recompile your source code to generate a new class file for UNIX, because the JVM running on UNIX
can understand the bytecode you generated on Windows. This is how the concept of “write once, run anywhere” is
implemented for a Java program.
The Java platform, also called Java runtime system, consists of two things:
The Java virtual machine (JVM)
The Java Application Programming Interface (Java API)
 
Search WWH ::




Custom Search