Debugging is the process of finding, understanding, and fixing errors in a program. Java developers use debugging techniques and tools to identify why a program is not working as expected.
Debugging means examining a program to find the cause of a problem and correcting it.
For example, consider this program:
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result = a / b;
System.out.println(result);
}
}
This program attempts to divide an integer by zero.
ArithmeticException when integer division by zero occurs.
Corrected version:
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 2;
int result = a / b;
System.out.println(result);
}
}
Java programs commonly encounter these types of errors:
| Error Type | Description | Example |
|---|---|---|
| Syntax Error | Code violates Java syntax rules. | Missing semicolon. |
| Compile-Time Error | The compiler cannot compile the code. | Using an undeclared variable. |
| Runtime Error | An error occurs while the program is running. | Integer division by zero. |
| Logical Error | The program runs but produces an incorrect result. | Using addition instead of multiplication. |
public class Main {
public static void main(String[] args) {
System.out.println("Hello")
}
}
The statement is missing a semicolon.
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
public class Main {
public static void main(String[] args) {
int length = 10;
int width = 5;
int area = length + width;
System.out.println("Area: " + area);
}
}
The program compiles and runs, but the formula is incorrect. The area of a rectangle is calculated by multiplying length by width.
int area = length * width;
One of the simplest debugging techniques is printing variable
values and messages using System.out.println().
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println("Value of a: " + a);
System.out.println("Value of b: " + b);
int result = a * b;
System.out.println("Result: " + result);
}
}
Value of a: 10
Value of b: 5
Result: 50
These messages help you check whether variables contain the expected values and whether the program follows the expected steps.
A breakpoint is a marker placed on a line of code where a debugger should pause program execution.
Breakpoints allow you to:
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);
}
}
You can place a breakpoint on the line
int sum = a + b; in your IDE.
When the debugger reaches that line, execution pauses before
the statement runs.
Debuggers provide controls for executing code step by step.
| Command | Meaning |
|---|---|
| Step Over | Executes the current line without entering a called method. |
| Step Into | Enters a method called by the current statement, when possible. |
| Step Out | Continues execution until the current method returns. |
| Continue | Resumes execution until another breakpoint or stopping condition. |
public class Main {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println(result);
}
}
If you pause at add(10, 20), Step Into can take
you into the add() method. Step Over executes
the call without opening the method's code.
A debugger lets you inspect the values stored in variables while a program is paused.
public class Main {
public static void main(String[] args) {
int price = 100;
int quantity = 3;
int total = price * quantity;
System.out.println("Total: " + total);
}
}
When debugging, inspect these variables:
| Variable | Expected Value |
|---|---|
| price | 100 |
| quantity | 3 |
| total | 300 |
If total contains an unexpected value, inspect
the calculation and the values of its input variables.
A NullPointerException can occur when code attempts
to use an object reference that is null in a way
that requires an actual object.
public class Main {
public static void main(String[] args) {
String name = null;
System.out.println(name.length());
}
}
The program throws a NullPointerException because
name does not refer to a String object.
public class Main {
public static void main(String[] args) {
String name = null;
if (name != null) {
System.out.println(name.length());
} else {
System.out.println("Name is not available.");
}
}
}
Name is not available.
When debugging a null reference problem, check where the reference is assigned and whether it can be null before use.
A stack trace provides information about an exception and the sequence of method calls leading to it.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:5)
Important parts of this message:
ArithmeticException identifies the exception type.
/ by zero describes the problem.
Main.java:5 identifies the source file and
line where the exception was reported.
An Integrated Development Environment (IDE) provides tools to compile, run, and debug Java programs.
Common Java development environments include:
An assertion checks whether a condition is true. It can help detect unexpected program states during development.
public class Main {
public static void main(String[] args) {
int age = 20;
assert age >= 0 : "Age cannot be negative";
System.out.println("Age: " + age);
}
}
If the assertion condition is false and assertions are enabled,
Java throws an AssertionError.
Enable assertions when running the program:
java -ea Main
-ea. Do not use assertions
to validate essential user input or enforce security checks.
System.out.println() to inspect variable values.NullPointerException in a String example.-ea.Choose the correct answer for each question.