简体   繁体   中英

Java compiler/eclipse not reconizing dead code

So I recently came accros this. I was creating an if-else-statement with as it's condition a final boolean variable. Eclipse immediatly told me that the else part of the code was unreachable and therefore dead code. This was my code(compacted).

public static void main(String[] args){
    final boolean state = true;
    if(state == true){
        System.out.println("A");
    }else{
        System.out.println("B");
    }
}

Then I though what would happen if the code stayed the same but the variable wasn't final anymore? So I tried that and this was what happened, nothing no warnings or errors. The code:

public static void main(String[] args){
    boolean state = true;
    if(state == true){
        System.out.println("A");
    }else{
        System.out.println("B");
    }
}

Now I'm wondering, why is the first case detected and flagged and the second case not?

Thank you in advance.

Try this as an alternative.

public class Test061 {
    public static void main(String[] args){
        int a = 0;
        if(1 == (a+1)){
            System.out.println("A");
        }else{
            System.out.println("B");
        }
    }
}

There are still no warnings. Why?

Because the compiler does not execute your code. It only can issue a warning when it sees some "constants" or "constant expressions". Apparently when you put final the compiler then knows that this value cannot change. While if you use "real variables" or "variable expressions", it doesn't know because it doesn't execute your code (to see what the value of state or (a+1) is at this line). So in the latter case, you get no warnings. Hope this makes more sense now.

Think of it this way: the compiler does some code analysis. The first basic pattern is detected by this analysis, while the second pattern is not (probably as it's not that basic).

final means that something cannot be changed, so likely it was flagged because it can never and will never reach that else statement.

On a side note, you never have to do if(Boolean == true) you can just do if(Boolean)

The compiler does some optimization for final variable as shown below and in that case compiler knows that else part will never reached because final variable can't be changed later.

if (true) {
    System.out.println("A");
} else {
    System.out.println("B");
}

Read more JLS §4.12.4. final Variables

Find more possibilities for Unreachable code compiler error

First example:

When we use final keyword with a variable and assign a value to the variable, then that value can't change again. final boolean state = true; It can't have a value of "false".

Second example:

Here the variable state is not final. It has the possibility to get a value of "false".

So the different behavior is because of the final keyword.

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