Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does intellij print "void" when using evaluate log for a breakpoint?

I wrote this Java code in intellij:

public class Main {
    public static void main(String[] args) {
        int[] a = {1,1,1,1,1,1};
        for(int i = 0; i < a.length; ++i) {
            a[i]++;
        }
    }
}

Next I set a breakpoint inside the for-loop like so.

Breakpoint

Next I go to the "view breakpoints" menu, which can be accessed by ctrl+shift+f8 and enter these settings for my breakpoint.

Breakpoint settings

Then I hit the debug button and my output is this:

void
1
void
1
void
1
void
1
void
1
void
1

Why is intellij printing "void" in the output?

like image 989
Coder-Man Avatar asked May 05 '18 12:05

Coder-Man


People also ask

Why debug is not working in IntelliJ?

To solve this, simply remove the jar of the debugged module from all modules' dependencies in the Project Structure. If you do not know which modules have the debugged module jar as dependencies, you can use some tools (Eg. Sublime Text, bash, ...) to search for the module name which is stored in Intellij *.

How do I debug a breakpoint in IntelliJ?

To do this, go to Settings/Preferences | Build, Execution, Deployment | Debugger and select Drag to the editor or click with middle mouse button. Clicking a breakpoint will then enable or disable it.

How do I show debug variables in IntelliJ?

You can click on the Run icon in the gutter area and select the Debug option. You can invoke context actions on the class or main method by using Alt+Enter and choose the Debug action. You can also start it from the Run menu, or by pressing Shift F9.

How do I change the debug value in IntelliJ?

Select a variable or a property of a complex object in the Debug window, press F2 or right-click and choose Set Value... from the context menu, and then specify a new value and press Enter .


1 Answers

It's because the println() method is declared as void. When you evaluate an expression in debug mode it generates a literal value from the expression's result. So, when you evaluate the expression System.out.println(a[i]), you are really telling the debugger to print the contents of the method's return value, which happens to be void. It so happens that within the method an additional println(int) method call is performed, making the actual evaluated expression essentially equivalent to this puedocode System.out.println(System.out.println(a[i])) (though of course in actuality Java doesn't treat void as a true type, so you wouldn't be able to actually execute the code as written).

The JavaDoc for the PrintStream.println(int) method is available here (for Java 9) and you can view the source code of the method here (from OpenJDK JDK version 8u40-b25 aka Java 8).


Consider the following methods:

void evaluatesToVoid(){
    final int[] a = {1,1};
    for(int i = 0; i < a.length; ++i) {
        System.out.println(a[i]);
    }
    
    return;
}

int evaluatesToSeven(){
     return 7;
}

When you evaluate a method call which returns a valid return type, for example the primitive type int, you will get the method's return value.

Using evaluate and log on evaluatesToVoid() would print this:

void
1
1

Using evaluate and log on evaluatesToSeven() would print this:

7
like image 162
Emily Mabrey Avatar answered Oct 18 '22 07:10

Emily Mabrey