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
System.out.println(System.out.println(a[1]))
.1
in console ?a[i]
?a[i]
. We are saying to log return value ofSystem.out.println(a[i])
. So, why ?System.out.println(a[i])
prints the value ofa[i]
before it returns?