7

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?

6
  • 2
    "Evaluate and log" means that it automatically prints the result. You're basically doing System.out.println(System.out.println(a[1])).
    – JJJ
    May 5, 2018 at 13:01
  • @JJJ the question should be why it is printing 1 in console ?
    – Number945
    May 5, 2018 at 13:14
  • 1
    @BreakingBenjamin ...because that's the value of a[i]?
    – JJJ
    May 5, 2018 at 13:33
  • @JJJ but we are not saying to log the value of a[i]. We are saying to log return value of System.out.println(a[i]) . So, why ?
    – Number945
    May 5, 2018 at 14:22
  • 1
    @BreakingBenjamin ...because System.out.println(a[i]) prints the value of a[i] before it returns?
    – JJJ
    May 5, 2018 at 14:23

1 Answer 1

3

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.