Java && 和 || 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20207997/
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
The difference between && and ||
提问by ragingbull
Im not understanding the difference between the && and the || in a certain regard.
我不明白 && 和 || 之间的区别 在某个方面。
If(a && b) { then do this }
Now if a is false then b Islooked at, but the condition is not tested.
现在,如果是假的,那么B是看了,但条件不测试。
But....
但....
If(a || b) { then do this }
Now, if a is true then b Is notlooked at, and the condition is not tested.
现在,如果 a 为真,则不查看b ,并且不测试条件。
Why is this. I thought the purpose of the && and the || was to sort of a help speed things along. If it could determine a result by testing the first condition, it wouldnt need look at the second condition. But in the case of && it does look at it. While in the case of || it dosent.
为什么是这样。我认为 && 和 || 的目的是 有点帮助加快事情的进展。如果它可以通过测试第一个条件来确定结果,则不需要查看第二个条件。但是在 && 的情况下,它确实会查看它。而在 || 的情况下 它剂量。
Do i have this right? If i do, why is this?
我有这个权利吗?如果我这样做,这是为什么?
回答by Sudhakar Tillapudi
&&
is used to perform and
operation means if anyone of the expression/condition
evaluates to false whole thing is false.
&&
用于执行and
操作手段,如果任何一个expression/condition
评估为假,整个事情都是假的。
so if any one of the condition becomes false it won't even continue further to check
||
is used to perform or
operation if anyone of the expression/condition
evaluates to true whole thing becomes true.
||
用于执行or
操作,如果任何一个 expression/condition
评估为真的整个事情都变成真的。
so it continues till the end to check atleast one condition to become true.
回答by CHowdappaM
&& is AND where as || is OR operator.
&& looks for false, means if first argument false, it wont test second whether it is true or false.
|| looks for true, means even though first argument false, it test for second. If both are false then false otherwise true. In && case both are true then true otherwise false.
&& is AND where as || is OR operator.
&& 查找 false,表示如果第一个参数为 false,则不会测试第二个参数是否为真。|| 寻找真,意味着即使第一个参数为假,它也会测试第二个。如果两者都为假,则为假,否则为真。在 && 情况下,两者都为真,否则为真。
回答by dasblinkenlight
To understand the answer, remind yourself of the truth tables of AND
and OR
operations:
要理解答案,请提醒自己AND
和OR
操作的真值表:
AND true | false
\--------+-------
true | true | false
------+-------+-------
false | false | false
OR true | false
\--------+-------
true | true | true
------+-------+-------
false | true | false
Note the second column for AND
and the first column for OR
: they have identical values. This is the key to understanding the reason why AND
ignores its second argument when the first evaluates to false
: that's when the second argument does not matter (the second column). The opposite is true for the OR
operator: now a false
in the first argument means that the second one does matter. However, a true
in the first argument (the first column) means that the second argument can be ignored.
请注意第二列 forAND
和第一列 for OR
:它们具有相同的值。这是理解为什么AND
在第一个评估为 时忽略第二个参数的关键false
:那是第二个参数无关紧要(第二列)的时候。OR
运算符的情况正好相反:现在false
第一个参数中的 a 意味着第二个参数确实重要。但是,true
第一个参数(第一列)中的 a 意味着可以忽略第二个参数。
I thought the purpose of the && and the || was to sort of a help speed things along.
我认为 && 和 || 的目的是 有点帮助加快事情的进展。
That's a smaller part of their purpose. Much bigger one is protection of right side of an expression based on its left side. Consider a situation where you want to guard against empty and null
strings. If &&
and ||
didn't "short circuit" the evaluation, you would be forced to write code like this:
这是他们目的的一小部分。更大的一个是基于其左侧的表达式右侧的保护。考虑一种情况,您要防止空null
字符和字符串。如果&&
并且||
没有“短路”评估,您将被迫编写如下代码:
if (s == null) return false;
if (s.length() == 0) return false;
The reason you can write
你可以写的原因
if (s == null || s.length() == 0) return false;
is the short circuiting: there is a guarantee that s.length()
would not be invoked on a null
string.
是短路:保证s.length()
不会在null
字符串上调用。
回答by Chris
a && b
can't be true if a is false, so if a really is false, there's no need to examine b.
a && b
如果 a 为假,则不可能为真,因此如果 a 真的为假,则无需检查 b。
a || b
can't be false if a is true, so if a really is true, there's no need to examine b.
a || b
如果 a 为真,则不能为假,因此如果 a 真的为真,则无需检查 b。
They're called short-circuit operators for that reason.
因此,它们被称为短路运算符。
If you're trying to defeat that behavior, use & and | instead.
如果您想阻止这种行为,请使用 & 和 | 反而。
回答by Chris
Hmmm... On the off chance the question is your confusion regarding the && operator evaluating the right-hand operand when the left-hand operand is false, then read this:
嗯......如果左手操作数为假时,问题是您对 && 运算符评估右手操作数的混淆,然后阅读以下内容:
Conditional And Operator (Java Language Specification)
Specifically, the first sentence:
具体来说,第一句话:
The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.
条件与运算符 && 类似于 &(第 15.22.2 节),但仅当其左侧操作数的值为真时才计算其右侧操作数。
and the last:
最后一个:
Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.
因此, && 在布尔操作数上计算与 & 相同的结果。它的不同之处仅在于右侧操作数表达式是有条件地而不是始终进行计算的。