Scala-布尔值-&与& ;, | vs || [英] Scala - Booleans - & vs &&, | vs ||
本文介绍了Scala-布尔值-&与& ;, | vs ||的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚注意到在Scala中,Boolean
支持&
和&&
.这两个运算符之间有区别吗? Scala文档对两者都使用完全相同的描述,所以我不确定.
I just noticed that in Scala Boolean
supports both &
and &&
. Is there a difference between these two operators? The Scala docs use the exact same description for both of them, so I wasn't sure.
推荐答案
&
和|
是严格的,而&&
和||
却是短路的:
&
and |
are strict while &&
and ||
are short-circuiting:
false && (throw new Exception()) => false
false & (throw new Exception()) => ex
true || (throw new Exception()) => true
true | (throw new Exception()) => ex
&
和|
的完整文档有注释来解释此行为:
The full documentation for &
and |
have a note explaining this behaviour:
即使结果已经存在,此方法也会同时评估a和b 在评估a之后确定.
This method evaluates both a and b, even if the result is already determined after evaluating a.
这篇关于Scala-布尔值-&与& ;, | vs ||的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文