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.
Setup steps (IntelliJ IDEA, Eclipse and VS Code are similar)
- Install JDK 17 or later.
- Install the IDE.
- Create a new Java project.
- Create a class named Main.
- Type the program below, then press Run.
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
- Set a breakpoint by clicking beside a line number.
- Start the program in Debug mode.
- Step over line by line and watch the variables panel. Compare it with your trace table.
- Fix the problem and run again.
How a Java program runs
When you press Run, the IDE does several jobs for you:
- Edit: you type the source code in a file such as
Main.java. - Compile: the Java compiler (
javac) checks the syntax and translates the source code into bytecode, saved inMain.class. Syntax errors are reported at this stage. - 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".
| Part | What it is |
|---|---|
| JDK (Java Development Kit) | Everything needed to write Java: the compiler, the JVM and the standard libraries |
| JRE / JVM | What is needed to run Java programs |
| IDE | The program you work in, which uses the JDK behind the scenes |
The debugger controls
| Control | What it does |
|---|---|
| Breakpoint | Pauses the program when it reaches that line |
| Step Over | Runs the current line and stops at the next one. If the line calls a method, the whole method runs |
| Step Into | Goes inside the method being called, so you can follow it line by line |
| Step Out | Finishes the current method and stops where it was called from |
| Resume | Runs on until the next breakpoint, or to the end |
| Variables panel | Shows the current value of every variable in scope |
| Watch | Follows 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:
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.