5

Eclipse 一直说最后一个 elseif 和 else 是死代码,但我不明白。

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

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

我是这样推理的:

  1. 如果 bote img0 和 img1 不为空,则 if 评估为真
  2. 如果它评估为假,那么
    • img0 为空或
    • img1 为空或
    • img0 和 img1 都为空。
  3. 如果 img0 不为 null,则第一个 elseif 评估为 true,如果评估为 false,则 img1 可能不为 null,或者 img0 和 img1 都可能为 null

我错过了什么,“死亡”在哪里?

提前致谢。

4

5 回答 5

4

以这两种方式查看您的代码的使用情况:-

方式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;
    }
}

在这种情况下,您肯定会dead code在一个块或另一个块上收到警告,因为您是在块之前设置值,并且编译器确信这些值在这些块的初始化和执行之间不会改变。

方式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;
    }
}

现在,在这种情况下,您不会收到dead code警告,因为 Compiler 不确定从何处show调用该方法。img0和的值img1可以是方法内的任何值。

  • 如果两者都是,则将执行null最后一个。else
  • 万一one of them is null,其中之一else if将被执行。
  • 并且,如果它们都不是null,您if将被执行。

笔记 : -

如果需要,您可以将 Eclipse 配置warnings为在某些情况下不显示,例如 - Unneccessary elseUnused Imports等。

转到 Windows -> 首选项 -> Java(在左侧面板上)-> 编译器 -> 错误/警告

于 2012-11-26T10:36:44.647 回答
1

Eclipse 可以计算出这些变量在 U 到达评估点之前的状态。所以它知道它的状态并由此设置其他条件“死”,例如

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;
}

只有第二个条件没有死,其他的都是。

于 2012-11-26T10:51:57.713 回答
0

这编译没有问题。您的错误必须在其他地方!

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";
}

您能否向我们展示更多上下文,例如变量的初始化位置以及可能更改的位置?

当我将示例更改为

    String img0 = null;
    String img1 = null;

除了 之外的所有内容都会出现死代码警告else,这是完全有道理的(因为分析代码表明只有else将被执行)。

于 2012-11-26T10:50:55.527 回答
0

当编译器发现您已经在创建实例并且其中一个对象永远不会为空时,有时会发生这种情况......例如

Image img1 = new Image(); 

在此之后,您正在编写 if else 如上所述。

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

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

在这种情况下,其他条件将永远不会达到,因为 img1 永远不能为空......

于 2012-11-26T10:56:27.100 回答
0

Eclipse 可以判断变量是否已设置。此代码没有给出关于未使用代码的警告。两个变量都不为空:

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();
    }
}

然而,这段代码确实给出了警告,因为 Eclipse 可以判断img0is null,因此 if 和第一个 else if 永远不会被命中:

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();
    }
}
于 2012-11-26T10:59:39.950 回答