Tino APCS

Compiling and Running a Program

Writing your program

Editors

A programmer writes the text of a program using a software program called an editor.

  • Most editors, like BlueJ, are known as IDEs (Integrated Development Environments) because they also include tools to compile, run, and debug programs.

Source code

The text of a program in a particular programming language is referred to as source code. The source code is stored in a file called the source file.

  • For example, in the DrawSquare example given earlier, source code would be created and saved in a file named DrawSquare.java.

Compiling your program

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 gpdraw.jar, have already been compiled into bytecode for you.

Figure 1.5 - From Source Code to Running Program

Errors

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.

If an error occurs while running the program (meaning it compiled successfully), 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

Dark Mode

Outline