Java 为什么 false && (false)?false:true 返回 true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18788063/
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-08-12 11:04:40  来源:igfitidea点击:

Why does false && (false)?false:true return true

javasyntaxternary-operator

提问by Gomino

Please don't look at the condition as they are here to ease the understanding of the behavior

请不要看条件,因为它们是为了便于理解行为而存在的

Why does result equals true ?

为什么 result 等于 true ?

boolean result = false && (false)?false:true;

I know we can solve the issue doing:

我知道我们可以通过以下方式解决问题:

boolean result = false && (false?false:true);

But I am just wondering why the first syntax is incorrect, looks like the '?' operator has more priority over '&&'

但我只是想知道为什么第一个语法不正确,看起来像 '?' 运算符优先于“&&”

采纳答案by Bathsheba

The ternary conditional( ?:) has lower precedencethan &&. So

所述三元条件?:)具有较低的优先级&&。所以

boolean result = false && (false)?false:true;

(having unnecessary parentheses); is equivalent to

(有不必要的括号);相当于

boolean result = (false && false) ? false : true;

Since (since false && falseis false), this reduces to

由于(因为false && falsefalse),这减少到

boolean result = false ? false : true;

which, of course, is true.

这当然是true

回答by GriffeyDog

false && (false)evaluates to false, so the last value of the ternary operator is returned, which is true.

false && (false)计算结果为false,因此返回三元运算符的最后一个值,即true

回答by Leviathan56

it is just some boolean algegra.

它只是一些布尔代数

False && false = true
false && true = false
true && true = true
true && false = false

So in the first case, it is like writting :

所以在第一种情况下,就像写:

if (false && false){
result = false
} else {
result = true
}

In your second case, it is like writting :

在你的第二种情况下,就像写:

result = false && (false == false);

and false == false returns true. So false && true returns false

并且 false == false 返回 true。所以 false && true 返回 false

回答by Jesper

Because

因为

boolean result = false && (false)?false:true;

is interpreted as

被解释为

boolean result = (false && (false))?false:true;

See: Java operator precedence. In the table you can see &&has higher precedence than ? :.

请参阅:Java 运算符优先级。在表中,您可以看到&&具有比 更高的优先级? :

回答by Rohan Pawar

1) && (logical AND)

1) &&(逻辑与)

Description :- Compares two expressions and returns true only if both evaluate to true. Returns false if one or both evaluate to false.

描述 :- 比较两个表达式,只有当两者的计算结果都为真时才返回真。如果一个或两个评估为假,则返回假。

The following list shows all possible combinations:

以下列表显示了所有可能的组合:

true && false // Evaluates false because the second is false
false && true // Evaluates false because the first is false
true && true // Evaluates true because both are true
false && false // Evaluates false because both are false

Syntax

句法

expression1 && expression2

表达式 1 && 表达式 2

Cllick here to know more About Logical AND

单击此处了解有关逻辑与的更多信息

2) || (logical OR)

2) || (逻辑或)

Description :- Compares two expressions and returns true if one or both evaluate to true. Returns false only if both expressions are false.

描述 :- 比较两个表达式,如果其中一个或两个表达式为真,则返回真。仅当两个表达式都为假时才返回假。

The following list shows all possible combinations:

以下列表显示了所有可能的组合:

true || false // Evaluates true because the first is true
false || true // Evaluates true because the second is true
true || true // Evaluates true because both are true
false || false // Evaluates false because both are false

Syntax

句法

expression1 || expression2

表达式 1 || 表达式2

Click here to know about Logical OR

单击此处了解逻辑 OR