1. first search for the syntax errors

2. swith to debug mode and Search for for runtime errors and logic errors:

I will show you HOW TO USE THE TOOLS by using following example code:

package Debugging;

public class Project1 {	
	
	int add(int number1, int number2) {
		int answer = number1 - number2;
		return answer;
	}
	
	int minus(int number1, int number2) {
		int answer = number1 - number2;
		return answer;
	}
	
	void main () {
		int result = add(2,5);
		int answer = minus(result, 1);
		System.out.println(answer);
	}
	
	public static void main(String[] argv) {
		new Project1().main();
	}
	
}
OUTPUT: -4

If you calculate by hand: 2 + 5 – 1, this should be 6. However, the program gives the output -4.

To analyse where this goes wrong, use the debug tool:

Beforehand you should already have searched on syntax errors as described above.

  1. Switch to “Debug Mode”
  2. Create breakpoints; Mark the parts you want to analyse, by double clicking on the left side bar:
    debug blue dots java

    A blue dot will appear (a blue dot is a breakpoint)

     

  3. Press “Debug as” Press or press directly the symbol if java already selected the right project
     debug as java application  shortcut debug
  4. By using these tools, it is possible to go through the code:
    Key Description
    F5 Executes selected line, whilegoing step-by-step into the line
    F6 Executes selected line, withoutgoing step-by-step into the line
    F7 Stepping out of the selected method
    F8 Resume program, until next breakpoint
    ** This is a “going back” tool. For example, useful when you want to go back in a method

    debug tools

  5. This window “variables” shows the variables you have defined in the programvariables debug mode
    After debugging line 6 of the method add, the answer is -3. This is already a weird result. Because add should give 2 + 5 = 7.
  6. Therefore, look at the method add, which shows what the mistake was. In this case a logic error. The “-” sign is wrong, because it is an add function.
    int add(int number1, int number2) {
    	int answer = number1 - number2;
    	return answer;
    }
  7. After changing the add-function the program gives the desired result.
    package Debugging;
    
    public class Project1 {
    	
    	int add(int number1, int number2) {
    		int answer = number1 + number2;
    		return answer;
    	}
    	
    	int minus(int number1, int number2) {
    		int answer = number1 - number2;
    		return answer;
    	}
    	
    	void main () {
    		int result = add(2,5);
    		int answer = minus(result, 1);
    		System.out.println(answer);
    	}
    	
    	public static void main(String[] argv) {
    		new Project1().main();
    	}
    
    }

    OUTPUT: 6