简体   繁体   English

为什么在显式布尔测试中会出现死代码警告,但在隐式测试中却没有

[英]Why is there a dead code warning on explicit boolean test, but not on an implicit one

I was wondering why the following implicit testing for boolean true produce no dead code warning, while explicit testing produces one? 我想知道为什么以下对布尔值的隐式测试不产生死代码警告,而显式测试产生一个?

For example: 例如:

public void noDeadCodeWarning()
{
    final boolean x = false;
    if ( x )
    {
      System.out.println("This is dead code");  // no warning
    }
}

public void hasDeadCodeWarning()
{
    final boolean x = false;
    if ( x == true )
    {
      System.out.println("This is dead code");  // yes warning
    }
}

I'm using eclipse mars. 我正在使用日食火星。 and Java 1.8 和Java 1.8

In second method the IDE evaluate the expression so it say that it is dead code but in first method you have passed Boolean value. 在第二种方法中,IDE评估表达式,因此它表示它是死代码,但在第一种方法中,您已经传递了布尔值。

According to IDE if statement need Boolean value as condition so it doesn't evaluate 根据IDE if语句需要布尔值作为条件,因此它不进行评估

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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