5

Eclipse keep stating that the last elseif and the else is dead code but I don't get it.

if (img0 != null && img1 != null) {
    code;

} else if (img0 != null) {
    code;
} else if (img1 != null) {
    code;
} else {
    code;
}

I reason like this:

  1. the if evaluates true if bote img0 and img1 is not null
  2. if it evaluates false then
    • img0 is null OR
    • img1 is null OR
    • both img0 AND img1 is null.
  3. the first elseif evaluates true if img0 is not null, if it evaluates false img1 could be not null OR both img0 AND img1 could be null

What am I missing, where is the "deadness"?

Thanks in advance.

evading
  • 3,032
  • 6
  • 37
  • 57
  • 3
    What's inside the code? Please provide a _compilable_ test that demonstrates the issue. Perhaps the code does matter. – John Dvorak Nov 26 '12 at 10:35
  • 4
    It must be inside your code, it compiles fine for me. Maybe a thrown exception or return statement? – Udo Klimaschewski Nov 26 '12 at 10:40
  • I suspect the conditions are not authentic either. The code inside should not matter (as long it's syntactically valid) since it doesn't get executed. Perhaps there's an extra closing curly bracket inside the `code;`? – John Dvorak Nov 26 '12 at 10:44
  • It is probably about the initializing of img0 and img1. Where does it happen? Inside the same method? The code inside the conditions shouldn`t matter. – crusam Nov 26 '12 at 10:46
  • What is the value of `img0` and `img1` you have set? And where? – Rohit Jain Nov 26 '12 at 10:47
  • 2
    Come up with a complete method that shows the problem because your code itself doesn't cause it. – Marko Topolnik Nov 26 '12 at 10:55
  • Ok, turns out to be an Eclipse glitch I think. After like the gazzilionth hitting save-all and update it disappeared. Does anyone know what to do with the question now? Should I delete it since it was not my code that caused it? – evading Nov 26 '12 at 11:08
  • @refuser.. It's your choice. If you want we can close this question for you. I think you won't be able to delete it, as it has got answers with 3 upvotes. – Rohit Jain Nov 26 '12 at 11:11
  • @RohitJain I don't know, it's probbably a good idea to close it, right? But since I'm a noob I'm not really sure what's appropriate. – evading Nov 26 '12 at 11:14
  • @refuser.. Probably we would close this as `Not a Real Question`. It's no problem, if it takes time to get it closed or even it does not get closed. Just leave it. – Rohit Jain Nov 26 '12 at 11:16

5 Answers5

4

Take a look at the use of your code in these two ways: -

Way 1: -

public static void main(String[] args)
{
    String img0 = null;
    String img1 = "Asdf";

    /** Currently there is no code here, that can modify the value of 
        `img0` and `img1` and Compiler is sure about that. 
    **/

    /** So, it's sure that the below conditions will always execute in a
        certain execution order. And hence it will show `Dead Code` warning in 
        either of the blocks depending upon the values.
    **/

    if (img0 != null && img1 != null) {
       // code;

    } else if (img0 != null) {
        //code;

    } else if (img1 != null) {
        //code;
    } else {
       // code;
    }
}

In this case, you would certainly get dead code warning on one block or the other, because you are setting the value just before the blocks, and the compiler is sure that the values won't change between the initialization and execution of those blocks.

Way 2: -

public static void main(String[] args)
{
    String img0 = null;
    String img1 = "Asdf";

    show(img0, img1);
}

public static void show(String img0, String img1) {

    /** Now here, compiler cannot decide on the execution order, 
        as `img0` and `img1` can have any values depending upon where 
        this method was called from. And hence it cannot give dead code warning.
    **/

    if (img0 != null && img1 != null) {
       // code;

    } else if (img0 != null) {
        //code;

    } else if (img1 != null) {
        //code;
    } else {
       // code;
    }
}

Now, in this case, you won't get dead code warnings, because Compiler is not sure, from where the show method might get invoked. Values of img0 and img1 can be anything inside the method.

  • In case both of them are null, the last else will be executed.
  • In case one of them is null, one of the else if will be executed.
  • And, in case none of them are null, your if will be executed.

Note : -

If you want, you can configure Eclipse not to show warnings for certain cases like - Unneccessary else, Unused Imports, etc.

Go to Windows -> Preferences -> Java (on Left Panel) -> Compiler -> Errors/ Warnings

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

Eclipse can figure out in which state these variables are before U are coming to evaluation point. So it knows its states and by this sets other conditions "dead", e.g.

String img0 = "";
String img1 = null;
if(img0 != null && img1 != null)
{
    int i = 0;
}
else if(img0 != null)
{
    int i = 10;
}
else if(img1 != null)
{
    int i = 100;
}
else
{
    int i = 1000;
}

Only second condition is not dead, every other is.

Romczyk
  • 361
  • 3
  • 12
0

This compiles with no problems. Your error must be somewhere else!

public static void main(String[] args) {
    String img0 = nullOrString();
    String img1 = nullOrString();
    if (img0 != null && img1 != null) {
       // code;
    } else if (img0 != null) {
       // code;
    } else if (img1 != null) {
       // code;
    } else {
       // code;
    }
}
public static String nullOrString() {
    return Math.random() > 0.5 ? null : "abc";
}

Can you show us some more context, like where the variables are initialized, and where they might be changed?

When I change my example to

    String img0 = null;
    String img1 = null;

there will be Dead Code warnings for everything besides the else, which makes total sense (because analyzing the code shows that only else will be executed).

jlordo
  • 37,490
  • 6
  • 58
  • 83
0

It sometimes happens when compiler figures out that you are already creating the instance, and one of the objects will never be null... e.g.

Image img1 = new Image(); 

and after this you are writing if else as stated above.

if (img0 != null && img1 != null) {
    code;

} else if (img0 != null) {
    code;
} else if (img1 != null) {
    code;
} else {
    code;
}

In this case else condition will never reach as img1 can never be null...

Vishal
  • 3,189
  • 1
  • 15
  • 18
0

Eclipse can tell if a variable has been set or not. This code gives no warnings about unused code. Both variables are not null:

public void test() {
    Bitmap img1 = Bitmap.createBitmap(48, 48, null);
    Bitmap img0 = Bitmap.createBitmap(48, 48, null);
    if (img0 != null && img1 != null) {
        test();
    } else if (img0 != null) {
        test();
    } else if (img1 != null) {
        test();
    } else {
        test();
    }
}

This piece of code, however, does give a warning, because Eclipse can tell img0 is null, thus the if and the first else if are never hit:

public void test() {
    Bitmap img1 = Bitmap.createBitmap(48, 48, null);
    Bitmap img0 = null;
    if (img0 != null && img1 != null) {
        test();
    } else if (img0 != null) {
        test();
    } else if (img1 != null) {
        test();
    } else {
        test();
    }
}
stealthjong
  • 10,858
  • 13
  • 45
  • 84