This is Lesson 1.4 of the Java programming course.
How Java programs are developed
Most programming languages use a specific set of commands that resemble English, but which must follow a prescriptive set of rules. By using commands that loosely resemble English it makes it easier for human programmers to give the computer the required instructions. These commands as written by the programmer is known as source code.
Because computers do not (yet) understand English, there needs to be some sort of translation into the language computers do understand, namely binary numbers. This translation is carried out by specially written "translation" programs which can be either (or both) of the following types:
- Compiler: this type of translator is invoked by the programmer and reads all the source code to produce a complete runnable version that will be directly understood by the computer, and which is known as machine code.
- Interpreter: this type of translator is invoked by the end user of the program, although they probably won't know about it as it will be done automatically. It reads the source code one (or more) command(s) at a time performing translation to machine code on the fly and running the translated code immediately. It then carries on through the remainder of the source code doing the translation and running it until the end of the program.
Because interpreters translate and run on the fly, they tend to run more slowly than equivalent programs that were compiled.
Compiling Java programs
Machine code is of necessity platform-specific (e.g., Windows, Linux, Max, different processors, etc.). Because one of the primary goals of Java is portability (i.e., platform independence), Java uses a modified version of the above approach which is summarised in these steps:
- The programmer writes the Java source code in the normal way. These source code files are typically given a file suffix of .java.
- The programmer invokes the compiler to create what is known as bytecode (this can be thought of as platform independent machine code). The bytecode is stored in a file with the same name as the source code except with a different file suffix, in this case .class
- The bytecode is delivered to the end user computer.
- The end user needs to already have a Java Virtual Machine (JVM) installed on their machine. They can then run the bytecode (which is interpreted by the JVM).
It can therefore be seen that Java programs are both compiled and interpreted.
There are separate JVMs for different platforms, but they are all capable of running the same compiled bytecode. It is this approach that gives Java its platform independence since the programmer only needs to develop, compile, and deliver one version of the program.
The next lesson will look at Java syntax and naming conventions.