Why is an extra semicolon not allowed after the return statement, when it is allowed for other statements?
Why multiple semicolon is not allowed after the return statement, when it is allowed for all other statement?
Simply because when you have a statement like
System.out.println();;
This means you have two statements, one is System.out.println();
and other statement is after the first semi colon
, it is empty and that's allowed BUT you can't have any empty statement or any other statement after the return
statement because it will never execute, in other words, its unreachable statement and you can't have unreachable statements in your code.
Same thing happens in this code too
if(a == b)
System.out.println();;
else
System.out.println();
that's because, when you have an else
statement, statement just before it should be if
statement which is not the case in above code snippet because statement just before else
statement is an empty statement
which is not allowed.
If you have parenthesis after the if statement
like
if(a == b) {
System.out.println();;
}
else
System.out.println();
you will get no errors because now empty statement is inside an if block
and the statement just before else
is if statement
and not the empty statement
which was the case when you had no parenthesis after if statement
Your code:
if (a == b)
System.out.println();;
else
System.out.println();
is equivalent to
if (a == b) System.out.println();
;
else System.out.println();
And you can't use an else
if the preceding statement is not an if
.