Unit 1 · Basics of Computer Programming

1.4.3Using an IDE

What an IDE is

An Integrated Development Environment combines an editor, compiler, runner and debugger. Benefits: syntax highlighting, code completion, instant error messages, project management and debugging.

Main.java - MyProjectPROJECT▾ MyProject ▾ src Main.java Student.java1public class Main {2 public static void main(...) {3 int total = 0;4 for (int i = 1; i <= 4; i++) {5 total = total + i;6 }7 System.out.println("Sum = " + total);8 }9}VARIABLESi3total3CONSOLESum = 10project treeeditor + breakpointdebugger paneloutput
The main parts of an IDE: project files, editor with a breakpoint, debugger variables and the output console

Setup steps (IntelliJ IDEA, Eclipse and VS Code are similar)

  1. Install JDK 17 or later.
  2. Install the IDE.
  3. Create a new Java project.
  4. Create a class named Main.
  5. Type the program below, then press Run.
Java
public class Main {
    public static void main(String[] args) {
        int n = 4;
        int total = 0;
        for (int i = 1; i <= n; i++) {
            total = total + i;
        }
        System.out.println("Sum = " + total);
    }
}

Expected output: Sum = 10.

Debugging skills to practise

  1. Set a breakpoint by clicking beside a line number.
  2. Start the program in Debug mode.
  3. Step over line by line and watch the variables panel. Compare it with your trace table.
  4. Fix the problem and run again.

How a Java program runs

When you press Run, the IDE does several jobs for you:

  1. Edit: you type the source code in a file such as Main.java.
  2. Compile: the Java compiler (javac) checks the syntax and translates the source code into bytecode, saved in Main.class. Syntax errors are reported at this stage.
  3. Run: the Java Virtual Machine (JVM) loads the bytecode and executes it. Runtime errors appear at this stage.

Because bytecode runs on a JVM rather than directly on the processor, the same .class file runs on Windows, macOS and Linux. This is the meaning of Java's slogan "write once, run anywhere".

PartWhat it is
JDK (Java Development Kit)Everything needed to write Java: the compiler, the JVM and the standard libraries
JRE / JVMWhat is needed to run Java programs
IDEThe program you work in, which uses the JDK behind the scenes

The debugger controls

ControlWhat it does
BreakpointPauses the program when it reaches that line
Step OverRuns the current line and stops at the next one. If the line calls a method, the whole method runs
Step IntoGoes inside the method being called, so you can follow it line by line
Step OutFinishes the current method and stops where it was called from
ResumeRuns on until the next breakpoint, or to the end
Variables panelShows the current value of every variable in scope
WatchFollows the value of an expression you choose, such as total / count

Reading error messages

Do not panic at a long error message; read it from the top. The first line names the problem, and the message gives a file name and line number:

Text
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Main.main(Main.java:7)

This says: an ArithmeticException (division by zero) happened in method main, in file Main.java, at line 7. Go to that line and ask which value could be zero. With compiler errors, fix the first error first: one missing bracket can cause a long list of false errors after it.

Remember

  • Java source code is compiled to bytecode, which the JVM runs.
  • A breakpoint pauses the program; stepping shows how each line changes the variables.
  • Read error messages for the error type, the file and the line number.