0
public class Main
{
    static String test(){
         if(true){
             return "true";
         }
         else{
             return "false";    // Why this is not an unreachable code?
         }
     }
    public static void main(String[] args) {
        System.out.println(test());
    }
}

I tried to compile this code example and was expecting an "unreachable code" compiler error. Why doesn't that happen?

Michael
  • 41,989
  • 11
  • 82
  • 128
  • It's impossible to detect all instances of unreachable code, so the language specification has to spell out which cases cause an error and which do not. – Matt Timmermans Feb 27 '23 at 13:51
  • 1
    BTW this question was certainly asked before... :- | e.g [Why does compiler recognize while(true) at compile time but not if(true)](https://stackoverflow.com/questions/30711897/why-does-compiler-recognize-whiletrue-at-compile-time-but-not-iftrue) – user16320675 Feb 27 '23 at 13:58
  • @JohannKexel the question is about why this is **NOT** an "unreachable code" compilation error. – f1sh Feb 27 '23 at 14:01
  • Does this answer your question? [Why Java identifies unreachable code only in case of while loop?](https://stackoverflow.com/questions/23977508/why-java-identifies-unreachable-code-only-in-case-of-while-loop) – Sören Feb 27 '23 at 15:51

2 Answers2

3

It is practically speaking unreachable, but the Java language designers to decide to permit it to allow for the use of feature flags. So it's not considered "unreachable" for the purposes of throwing a compiler error, but for the purposes of the JIT compiler or something, it might get treated as if it were.

As per the JLS

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.

Michael
  • 41,989
  • 11
  • 82
  • 128
3

Because if (true) and if (false) are a special case. The compiler specifically allows them, by design, because often developers will want to temporarily enable or disable code. It’s kind of like Java’s version of #ifdef in C.

From the Unreachable Statements section of the Java Language Specification:

…the superficially similar case:

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.

VGR
  • 40,506
  • 4
  • 48
  • 63