I put an extra semicolon after the semicolon of System.out.println
:
System.out.println();;
Which was legal to Java compiler, so I checked for other statements and they were all legal too. So when I searched and found these links:
Why does Java not show an error for double semicolon at the end of a statement?
Compiler doesn't complain when I ended a line with two semicolons. Why?
When would you put a semicolon after a method closing brace?
I came to understand that an extra semicolon means an extra empty statement.
But when I put an extra semicolon after a return
statement, I got a compile time error. I came to the conclusion that the return
statement is considered to be the last statement in the flow of execution, so putting an extra statement after the return
is illegal.
The same thing happens in this code too:
if(a == b)
System.out.println();;
else
System.out.println();
Inside the if
statement System.out.println();;
gives a compile time error, because the compiler is expecting elseif
or else
. Am I right or is is there some other reason?