Next page | Contents page |

Compilation vs interpretation

With most traditional programming languages, the source code of a program would be compiled (a translation process) to make object modules consisting of instruction codes for the target hardware processor, on which the program will run. A number of such object modules would then be linked together as a separate step to make the runnable application. In order to run the same program on different hardware the source code must be run through a different compiler and linker, and often it must first be edited for that to work, because the language is slightly different on the other machine. There has been a lucrative business for programmers "porting" programs from one type of machine to another, even though the programming language is supposedly the same on both.

At the other extreme, popular in early microcomputers of the 1970s and 80s, source code can be interpreted statement by statement as it runs. The interpreter is a program held in ROM (read-only memory) so that it is instantly available when the machine is switched on. The translation process is inevitably much slower because the source code has to be parsed every time the program runs.

A Java compiler takes source code (in .java files) and translates it to an imaginary instruction set that cannot be run directly by any real hardware processor but is designed to be very efficient to interpret. This "byte code" is stored in .class files. There is no linking step. A Java Virtual Machine (JVM) is generally available for each real processor and that interprets the byte code to run the application. It is told which .class file has the starting point of the application. Whenever the code refers to another class that is then loaded by the JVM from its .class file. Classes are not loaded until they are needed. Once loaded they do not have to be loaded again, so the process is quite efficient. Byte code, in .class files, cannot be edited directly. There is a security system by which the JVM can tell if a .class file has been tampered with, and stop it loading.

The byte code is the same for all real machines but a different JVM will be available for each. In that way Java is totally portable and no "porting" conversion is ever needed (provided that the programmer has taken account of certain system-dependent things which we will point out when we come to them in this course).

Next page | Contents page |