Java: Java Virtual Machine

After you read this section, you should be able to understand how it's possible for a Java applet/application to run on many different machines. The key to Java's portability and security is the Java Virtual Machine.

The Java Virtual Machine

Most compilers translate from the source language (eg, C or Pascal) into machine language for one specific type of machine (eg, Pentium or Sun). The machine language for a Sun computer is not the same as for a Macintosh, or a Windows machine. You cannot use the same machine code program on different kinds of machines.

Most compilers produce code for a real machine

The Java compiler produces code for a virtual machine.

Java compilers translate applets in two steps. The first part of the translation is from Java source to Java Virtual Machine (JVM) machine code. This may seem strange because there isn't any real machine that can use JVM instructions yet!

JVM code is designed so that it is easy to translate it into machine instructions for real machines, so the second part of the translation to real machine instructions is done in the browser on the user's machine. The browser takes the applet JVM code that it gets from the server and translates from JVM code to the machine code the browser is using, eg, a Pentium. This compiler in the browser is often called a "JIT compiler". JIT stands for "Just In Time", and it means that the last part of the compilation is done just before running the program. The diagram below shows how the same applet JVM code is used by browsers on different machines.

Two step compilation allows Java applets to run on many kinds of machines.

Other languages can use the JVM

See for a list of languages that generate code for the Java Virtual Machine.

Java portability

So applets are really JVM code that is translated into the correct machine code by the browser. That's why applets can run on any machine, and C and C++ programs cannot.

This same two step translation can be used for Java applications as well as applets. If a program is in JVM code, then it is portable, and can be run on any machine that has a JIT compiler on it.

C and C++ object programs are not portable because they are always translated into the machine language for a specific machine. If you are very careful when you write your C programs, you may be able to compile and run the source program on many machines, but there are often big problems. For example, how big is a C int? On some machines it is 16 bits, on some machines it is 32 bits, and some it is 64 bits. Java has defined the basic types very exactly (eg, ints are always 32 bits). Another big problem is that lack of standard library functions in C/C++. There are some standard library functions for C/C++, but they are very small compared to Java, and don't do many of the things that programmers need to do (eg build a graphical user interface).

Portability is extremely important to many companies, since large companies usually have many kinds of computers. If a company can "write once and run everywhere" they can save an enormous amount of money. If a developer can do this, they will have a much larger market for their software.