8

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:

  1. Why does Java not show an error for double semicolon at the end of a statement?

  2. Compiler doesn't complain when I ended a line with two semicolons. Why?

  3. When would you put a semicolon after a method closing brace?

  4. Why does code with successive semi-colons compile?

  5. Semicolon at end of 'if' statement

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52

4 Answers4

9

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

Yousaf
  • 27,861
  • 6
  • 44
  • 69
4

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

When you use :

System.out.println();;

A new semicolon makes a new empty statement and it is ok to the compiler. But when you have

return; 

It is not allowed because you cannot put any more statements after the return statement because when the method returns, it stops there and the statements after it would never get called so it is not allowed!

Joza100
  • 338
  • 4
  • 16
1

The Java Language Specification §14.21 says:

It is a compile-time error if a statement cannot be executed because it is unreachable.

An extra semicolon (inside a method) is parsed as a separate "empty statement" (JLS §14.6). If you return from the method, those later statements aren't reachable. Hence the compiler is required to give an error.

Interestingly, the reachability rules have an exception to make it convenient to use an if statement for conditional compilation like C's #if. So merely putting an always-true if statement before the return is enough to satisfy the JLS that any following statements are "reachable":

if (true) return;;;;;;;;;;;;;;;;;;;;System.out.print("'reachable', but not really");;;

(Those extra semicolons are physically after the return;, but semantically, they are after the if.)

Other statements which, like return, control the program flow, can also cause statements to be unreachable. These extra semicolons are also not allowed:

  • throw null;;;;;;;;;;; — because the thrown NullPointerException exits the block
  • while (true);;;;;;;;;;;;;; — because the loop never exits
  • while (true) { break;;;;;;;;;;;; } — because the loop always exits early
  • while (true) { continue;;;;;;;;; } — because the loop always repeats early
  • while (false) { ;;;;;;;;;;;;;;;; } — because the loop never runs at all!
Boann
  • 48,794
  • 16
  • 117
  • 146