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.
Next I go to the "view breakpoints" menu, which can be accessed by ctrl+shift+f8 and enter these settings for my breakpoint.
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?
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