Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unreachable code compiler error [duplicate]

The following code gives an unreachable statement compiler error

public static void main(String[] args) {     return;     System.out.println("unreachable"); } 

Sometimes for testing purposes a want to prevent a method from being called, so a quick way to do it (instead of commenting it out everywhere it's used) is to return immediately from the method so that the method does nothing. What I then always do to get arround the compiler error is this

public static void main(String[] args) {     if (true) {         return;     }     System.out.println("unreachable"); } 

I'm just curious, why is it a compiler error?? Will it break the Java bytecode somehow, is it to protect the programmer or is it something else?

Also (and this to me is more interesting), if compiling java to bytecode does any kind of optimization (or even if it doesn't) then why won't it detect the blatant unreachable code in the second example? What would the compiler pseudo code be for checking if a statement is unreachable?

like image 210
ughzan Avatar asked Feb 14 '12 11:02

ughzan


People also ask

Is unreachable statement a compile error?

An unreachable Statement is an error raised as part of compilation when the Java compiler detects code that is never executed as part of the execution of the program. Such code is obsolete, unnecessary and therefore it should be avoided by the developer.

Is unreachable code a compile time error?

If any code can not be executable in any of the possible flows, then it is called unreachable code. Unreachable code in java is a compile time error.

What is unreachable code in compiler design?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.


1 Answers

Unreachable code is meaningless, so the compile-time error is helpful. The reason why it won’t be detected at the second example is, like you expect, for testing / debugging purposes. It’s explained in The Specification:

if (false) { x=3; } 

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false; 

and then write code such as:

if (DEBUG) { x=3; } 

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

Reference: http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.21

like image 182
André Avatar answered Oct 05 '22 23:10

André