A programmer writes the text of a program using a software program called an editor. The text of a program in a particular programming language is referred to as source code, or you can simply use source or code individually. The source code is stored in a file called the source file. For example in the DrawSquare
example given above, source code would be created and saved in a file named DrawSquare.java
.
Compiling is the process of converting a program written in a high-level language into the bytecode language the Java interpreter understands. A Java compiler will generate a bytecode file from a source file if there are no errors in the source file. In the case of DrawSquare
, the source statements in the DrawSquare.java
source file would be compiled to generate the bytecode file DrawSquare.class
. Classes inside a package, such as the gpdraw.jar, have already been compiled into bytecode for you.
Figure 1.5 - From Source Code to Running Program
Errors detected by the compiler are called compilation errors. Compilation errors are actually the easiest type of errors to correct. Most compilation errors are due to the violation of syntax rules. These are the basic rules of languages that programmers must follow so that the interpreter understands what to do. It is similar to grammar in a spoken language and varies from language to language.
The Java interpreter will process the bytecode file and execute the instructions in it.
If an error occurs while running the program, the interpreter will catch it and stop its execution. Errors detected by the interpreter are called run-time errors. Run-time errors are usually caused by a fault in the logic of the program, such as accidentally causing the computer to try and divide a number by zero.
Figure 1.6 - Edit-Compile-Run Cycle for a Java Program