2
if( 3 % 3 == 0){
    return 3;;
}

Java says unreachable statement.

I understand that java parses return 3;; as

return 3;  
        ;

So It says unreachable statement. But it is empty statement. Why does java care about it?

user207421
  • 305,947
  • 44
  • 307
  • 483
Gibbs
  • 21,904
  • 13
  • 74
  • 138

1 Answers1

1

Simple: the Java compiler treats an empty statement as just a statement. As this section from the Java language specification indicates:

Statement:

   StatementWithoutTrailingSubstatement
   LabeledStatement
   IfThenStatement
   IfThenElseStatement
   WhileStatement
   ForStatement

StatementWithoutTrailingSubstatement:
   Block
   EmptyStatement
   ExpressionStatement
   AssertStatement
   SwitchStatement
   ...

And the definition of unreachable statements applies to all types of statements. Note specifically the following:

An empty statement can complete normally iff it is reachable.

M A
  • 71,713
  • 13
  • 134
  • 174