简体   繁体   English

if(x) 和 if(x==true) 一样吗?

[英]Is if(x) same as if(x==true)?

Ok, so let's say I have an if statement and a boolean x:好的,假设我有一个 if 语句和一个布尔值 x:

if (x) {
    // some stuff
}

What happens here?这里会发生什么? Does this mean the same thing as if(x == true) ?这是否与if(x == true)

Yes,是的,

if(x) {

}

is the succinct equivalent of是简洁的等价物

if(x == true) {

}

As @Sotirios points out, they are different at the bytecode level.正如@Sotirios 指出的那样,它们在字节码级别是不同的。 Consider the following Java class:考虑以下 Java 类:

class Test { 
   public void foo() { 
      boolean x = true;
      if(x == true) { 
      }
   }
}

emits:发出:

  public void foo();
    Code:
       0: iconst_1      
       1: istore_1      
       2: iload_1       
       3: iconst_1      
       4: if_icmpne     7
       7: return 

vs对比

class Test { 
   public void foo() { 
      boolean x = true;
      if(x) { 
      }
   }
}

which emits:它发出:

  public void foo();
    Code:
       0: iconst_1      
       1: istore_1      
       2: iload_1       
       3: ifeq          6
       6: return  

I don't think this has any bearing on the performance or correctness of the program.我认为这对程序的性能或正确性没有任何影响。

Yes, it is.是的。

if checks if the condition is true, and if it is, executes the body statement. if检查条件是否为真,如果为真,则执行主体语句。 So x needs to be a boolean expression.所以x需要是一个boolean表达式。

In fact x is equivalent to x == true if x is a boolean expression, so you should use x as it is more concise.其实x相当于x == true ,如果x是一个布尔表达式,所以你应该使用x ,因为它是更简洁。 This becomes clearer if the variable has a proper name for a boolean, eg如果变量具有布尔值的正确名称,这将变得更加清晰,例如

if (this.isVisible()) {
    this.hide();
}

vs.对比

if (this.isVisible() == true) {
    this.hide();
}

The first one is better to read.第一个更好读。

是的,这与 if(x == true) 的含义相同。

如果x可以解析为布尔值,并且值为“true”,则将执行“其他内容”。

Yes, it is same as if(x == true) .是的,它与if(x == true) In fact if(x) is recommended.事实上if(x)是推荐的。

See the examples in The if-then and if-then-else Statements in The Java tutorial.请参阅 Java 教程中的 if-then 和 if-then-else 语句中的示例。

The return of both is a boolean, in this case both return true两者的返回都是一个布尔值,在这种情况下都返回真

For example:例如:

x= true. x=真。

Evaluating:评估:

a) if(x) a) 如果(x)

b) if(x==true) b) 如果(x==真)

Both return true, the first because is the value directly and the second because the expression it is true (true==true).两者都返回真,第一个因为是直接的值,第二个因为表达式为真(真==真)。

Hope this can help you too...希望这也能帮到你...

http://www.homeandlearn.co.uk/java/boolean_values.html http://www.homeandlearn.co.uk/java/boolean_values.html

Jep this is right.杰普这是对的。

There is also a short form that could be interesting for you:还有一个简短的表格,您可能会感兴趣:

It means if x is true then return true, else return false.这意味着如果 x 为真则返回真,否则返回假。

(x) ? return true : return false;

Short answer: yes.简短的回答:是的。

An if statement takes a boolean expression as its condition. if语句将布尔表达式作为其条件。 That can be the result of an operator which returns a boolean value such as == , or that can be a boolean variable.这可以是返回布尔值(例如==的运算符的结果,也可以是布尔变量。 Basically, a boolean expression is anything which you can assign to a boolean variable:基本上,布尔表达式是您可以分配给布尔变量的任何内容:

boolean b = ?;

Therefore, x == true is somewhat redundant, as x on its own already suffices: those two boolean expressions are equivalent .因此, x == true有点多余,因为x本身就足够了:这两个布尔表达式是等价的 It's quite common for new programmers though to write the == true , as it "reads" more naturally: if x is true, then do...对于新程序员来说,编写== true是很常见的,因为它更自然地“读取”:如果x为真,则执行...

However, you should actually read that as if (x == true) is true, then do... as that's what's actually happening.但是,您实际上应该将其读为if (x == true)为真,然后执行...因为这就是实际发生的情况。 Here, you see where the redundancy comes from.在这里,您会看到冗余的来源。 More experienced programmers know this and prefer writing x and !x as opposed to x == true and x == false respectively.更有经验的程序员知道这一点,并且更喜欢分别编写x!x而不是x == truex == false It doesn't matter for the compiler, but it matters for the programmer/maintainer when he has to read:这对编译器无关紧要,但对程序员/维护人员来说很重要,因为他必须阅读:

if ( ((x == true) && (y == true)) || (z == false) ) { ... }

as opposed to:与:

if ( (x && y) || !z ) { ... }

Does this mean the same thing as if(x == true) ?这是否与if(x == true)

Yes if(x == true) will give same results as if (x) .是的if(x == true)将给出与if (x)相同的结果。 It is based on simple logic:它基于简单的逻辑:

  • if x is true condition x == true is same as true == true which is evaluated to true which is same as original value of x如果xtrue条件x == truetrue == true相同,它被评估为与x原始值相同的true
  • if x is false condition x == true is same as false == true which is evaluated to false which again is same as original value of x如果xfalse条件x == truefalse == true相同,后者被评估为false ,再次与x原始值相同

So regardless of value of x expression if (x==true) is equivalent to if (x) .所以不管x表达式的值, if (x==true)等价于if (x)

Yes, both are Same & True .是的,两者都是Same & True And give you the same result.并给你同样的结果。

if(x) is more appropriate

Reason, In Programming language, any number but not 0 is considered as True .原因,在编程语言中,除0 之外的任何数字都被视为True

Suppose I take a number which is 5 :假设我取了一个数字5

if(x) or if(5)

because 5 is not equal to 0 or you can say 5!=0 that why it's True.因为5不等于0或者你可以说5!=0那为什么它是真的。

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

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