简体   繁体   中英

Why else case of if doesn't make any error when the if argument is already set as true in java

Why java program doesn't make error in the if else case when the if argument is true. Why doesn't it make any exception. for example, here method1 and method2 do not make any (compilation) error even if it is having unreachable statements, but method3 makes compilation error. First read the code carefully and provide the answer.

    public int method1() {
        if(true) {
            return 1;
        } else {
            return 2;//unreachable statement but doesn't make exception
        }
    }

    public int method2() {
        if(true) {
            return 1;
        } else if (true) {
           return 2;//unreachable statement but doesn't make exception
        } else {
            return 3;//unreachable statement but doesn't make exception
        }
    }

    public int method3() {

        if(true) {
            return 1;
        } else if (true) {
           return 2;//unreachable statement but doesn't make exception
        } else {
            return 3;//unreachable statement but doesn't make exception
        }

        return 3;//unreachable statement but makes exception
    }

doesn't java support a strict compilation? What is the priciple behind this problem?

The language allows conditional compilation by making a special case for if-then-else. This makes it easy to turn on or off blocks of code at compile-time.

From the Java Language Specification's section on unreachable statements :

   As an example, the following statement results in a compile-time error:

          while (false) { x=3; }

   because the statement x=3; is not reachable; but the superficially similar case:

          if (false) { x=3; }

   does not result in a compile-time error. 

And:

   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.

I would say that the compiler writers decided that the first cases weren't worth generating the error for, while the last was. I can only surmise what they were thinking, but I suspect that while they could determine reachability in the first two cases, it's highly unlikely that someone would accidentally write code like this. It's more likely that it would be introduced explicitly as a debugging mechanism. In addition, the cost of adding the evaluation to detect such an error (which is an edge case), probably isn't worth it. In any case that error is just annoying, not helpful. In the last case a programmer could easily write code like this and not realize that the final statement was unreachable, thus the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM