IDE (eclipse) debug execution, step execution (Java)

Introduction

This article is a record of what I understand for those who are currently studying Java.

In many cases, Java code is debugged and executed in an integrated development environment, and the operation is confirmed by step execution, so I will describe the series of steps.

Install eclipse

Install eclipse, which is currently free to use in a Java integrated development environment. You can download it from the link below.

https://mergedoc.osdn.jp/

Select the latest version of eclipse 2020 at this time.

スクリーンショット 2020-05-15 10.17.48.png

Then the screen of the following image will appear. This time I will install it according to my environment. Since it is a 64-bit CPU on a Mac and a Java environment is required, select the red frame below.

スクリーンショット 2020-05-15 10.27.14.png

You will be taken to the download page and the download will start automatically. After downloading, open the file and move it to the application.

スクリーンショット_2020-05-15_10_36_25.png

Creating a class for debugging

You have now installed it on your Mac. Let's start it immediately. Then a screen like the image will be displayed, so select Create Java Project.

スクリーンショット_2020-05-15_10_54_32.png

If you select Create, the image screen will appear. This time I want to run the Java file and debug it, so just enter the project name and you're done.

スクリーンショット_2020-05-15_10_58_45.png

When you're done, your project will be created, so let's create a class within the project. Right-click and select New, Class to display the creation screen.

スクリーンショット_2020-05-15_11_06_15.png

When the creation screen is displayed, name it Main and check public static void main (String args []) to complete.

スクリーンショット_2020-05-15_11_10_19.png

Now that the class is created, enter the following code into the eclipse editor.

public class Main{
    int instans = 60;
    static int global = 95;

    static void debug(){
        System.out.println("Debug execution");
    }

    void print(){
        System.out.println("print()");
    }

    public static void main(String args[]){
        Main.debug();
        System.out.println("main start");

        int x = 50;

        String str = "Character type";

        Main m = new Main();

        x = 100;

        str = "Character type conversion";

        m.print();

        System.out.println("main end");
    }
}

Let's debug this code and see how it works.

breakpoint

From here, you can start debugging, but in order to run debugging, you must first specify where to stop the program. That is called a breakpoint. Let's actually set a breakpoint in eclipse.

System.out.println("main start");

This time, set a breakpoint at the above location. Right-click on the left side of the number of lines of code above in the eclipse editor to bring up the menu and select Toggle Breakpoint.

スクリーンショット_2020-05-15_11_33_58.png

Since you can debug with this setting, select the bug mark on the upper left of eclipse. Debug execution starts here.

スクリーンショット_2020-05-15_11_43_40.png

This will launch the debug function. And the processing is stopped at the breakpoint set earlier. Therefore, the output of "debug execution" that operates before the breakpoint is output, and the f "main end" below is not output. Therefore, you can see that the process is stopped at the breakpoint placed between the two processes.

スクリーンショット_2020-05-15_11_47_15.png

Step execution

Up to this point, we have stopped processing, but let's perform step execution that advances processing line by line. Click the step over button in the continuation of the above.

スクリーンショット_2020-05-15_11_55_58.png

After clicking, the stopped process is executed on one line, and the selected state is reflected on the next line. Breakpoint

System.out.println("main start");

Since one line was executed by step execution, main start is displayed in the output column.

スクリーンショット_2020-05-15_13_02_56.png

Now that you have an image of step execution that moves line by line, let's press the step over button twice to proceed. There is a list of variables on the right side of the editor, but the contents have been updated.

スクリーンショット_2020-05-15_13_13_19.png

Variables are defined and initialized by performing two step executions.

int x = 50;
String str = "Character type";

By passing these two processes, the value of the variable currently held can be displayed. Click the stepover button three more times to proceed.

スクリーンショット_2020-05-15_13_17_39.png

There is a process in which the values of variables x and str are updated by performing step execution three times.

x = 100;
str = "Character type conversion";

Therefore, the current value of the variable list has been updated.

By the way, the variable m is defined in the interim process, and the created instance is assigned.

Main m = new Main();

The Main class has an instance variable instance, so you can see that it is also defined. Click the right arrow for the variable name m in the variable list.

スクリーンショット_2020-05-15_13_25_18.png

You can also check the instance variables like this. By the way, the static variable is not displayed because the static variable is an independent element and the variable m does not have it, so it is not displayed.

At the end

In this article, I described the procedure to debug execution in the IDE and check the operation line by line in the important step execution.

Since you can see the state of the value at the time when the program is watching during execution, you can check whether the value you intended is entered at any time when checking the operation, so incorporate debug execution How about trying it?

Recommended Posts

IDE (eclipse) debug execution, step execution (Java)
Java debug execution [for Java beginners]
java Eclipse How to debug javaScript
2017 IDE for Java
Java (eclipse) installation procedure
Java instruction execution statement
Eclipse ~ Java project creation ~
Is Eclipse IDE dying?
To debug in eclipse
Parallel execution in Java
Null-safe program in Java (Eclipse)
Java development environment (Mac, Eclipse)
[Java] Spring AOP execution order
Java tips --Spring execution Summary
First Java development in Eclipse
External process execution in Java