java 假布尔值 = 真?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7883502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:49:32  来源:igfitidea点击:

False boolean = True?

javaboolean

提问by Marwen Hizaoui

I found this code in a book and I executed it in Netbeans:

我在一本书中找到了这段代码,并在 Netbeans 中执行了它:

boolean b = false;
if(b = true) {
    System.out.println("true");
} else {
    System.out.println("false");
}

I just don't understand why the output of this code is true, Can anyone enlighten me please, Thanks.

我只是不明白为什么这段代码的输出是正确的,请任何人赐教,谢谢。

回答by Kirk Woll

It's missing the double-equals. So it's doing an assignment instead of an equality comparison (and remember, the return value of an assignment is the new value). In most cases, the fact that most types are not boolean means the result is not a boolean and so it becomes illegal for an ifstatement, resulting in a compiler error. However, since the type here is already a boolean, the assignment results in a boolean and so the safety-check fails. Thus, b = truemeans that bis assigned the value trueand this is the value that is returned and checked by the ifstatement.

它缺少双等号。所以它正在执行赋值而不是相等比较(请记住,赋值的返回值是新值)。在大多数情况下,大多数类型不是布尔值的事实意味着结果不是布尔值,因此对于if语句来说它变得非法,从而导致编译器错误。然而,由于这里的类型已经是一个布尔值,赋值结果是一个布尔值,因此安全检查失败。因此,b = true意味着b被分配了值true,这是由if语句返回和检查的值。

回答by Mysticial

This is because the if-statement condition isn't a comparison. It's an assignment:

这是因为 if 语句条件不是比较。这是一个任务:

if(b = true)

Which will always return true. So it will always print true.

这将始终返回 true。所以它总是会打印true

If you wanted to do a comparison, you need to use ==.

如果要进行比较,则需要使用==.

回答by George

In your "if" statement you are assigning the value "true" to b. You should check the value by using the comparison operator "==".

在您的“if”语句中,您将值“true”分配给 b。您应该使用比较运算符“==”来检查该值。

boolean b = false;

if(b == true)
{
   System.out.println("true");
}
else
{
   System.out.println("false");
}

回答by FailedDev

 if(b = true)

Well it is true because = is the assignment operator and not the equality operator ==

嗯,这是真的,因为 = 是赋值运算符而不是相等运算符 ==

回答by Paul Tomblin

if (b = true)assigns the value trueto b, and then acts on that true value. That's the danger of C-like languages, that if you leave out the second =when you meant to compare, you get an assignment instead, and an unexpected result.

if (b = true)将值分配trueb,然后对该真实值进行操作。这就是类 C 语言的危险,如果您=在打算比较时忽略了第二个,则会得到一个赋值,并得到一个意想不到的结果。

回答by Mathias Bak

It is because it is an assignment. You don't do a check on b, you just assign true to it.

因为这是一个任务。您不对 b 进行检查,您只需为其分配 true 即可。

回答by Gabriel

Think of int x having 2 possible values, either 1 or 0. If x = 1, then do this. Else, (x = 0) then do something else. Boolean is just saying it is either one condition, or not that condition.

考虑 int x 有 2 个可能的值,1 或 0。如果 x = 1,则执行此操作。否则,(x = 0)然后做其他事情。Boolean 只是说它要么是一个条件,要么不是那个条件。