Java 8 Boolean.logicalOr 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41736767/
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
Java 8 Boolean.logicalOr method
提问by Shubham Batra
In Java 8 new methods in Boolean
class have been added.
在 Java 8Boolean
类中添加了新方法。
Let's just talk about one of them
让我们只谈其中之一
public static boolean Boolean.logicalOr(boolean a , boolean b)
public static boolean Boolean.logicalOr(boolean a , boolean b)
Now, my question is, Why were they needed?
现在,我的问题是,为什么需要它们?
What's the difference between the following two cases.
以下两种情况有什么区别。
boolean result = a || b;
or Boolean result = Boolean.logicalOr(a,b);
boolean result = a || b;
或者 Boolean result = Boolean.logicalOr(a,b);
What's so special about Boolean.logicalOr()
and when should I prefer one over the other.
有什么特别之处Boolean.logicalOr()
,什么时候我应该更喜欢一个而不是另一个。
回答by Roland
Mainly those methods are there for your convenience and to make the code more readable by using the method references in lambdas/streams. Let's look at an example:
这些方法主要是为了您的方便,并通过使用 lambdas/streams 中的方法引用使代码更具可读性。让我们看一个例子:
Stream.of(/* .. some objects .. */)
.map(/* some function that returns a boolean */)
.reduce(Boolean::logicalOr);
trying to write this with a || b
:
尝试用以下方法编写a || b
:
Stream.of(...)
.map(...)
.reduce((a, b) -> a || b); // logicalOr is actually using ||
not that readable, right?
不那么可读,对吧?
As Sotirios Delimanolis stated in the comment, you may also want to have a look at the javadoc and follow @see BinaryOperator. Or have a look at the function package summary javadoc.
正如 Sotirios Delimanolis 在评论中所述,您可能还想查看 javadoc 并关注@see BinaryOperator。或者看看函数包摘要 javadoc。
回答by Sebastian
It has to do with method references. Like this you can use the ||
(logical or) operator also in lambdas.
它与方法引用有关。像这样,您也可以||
在 lambda 中使用(逻辑或)运算符。
In this manner there also other new functions like Objects.isNull
etc.
通过这种方式,还有其他新功能,如Objects.isNull
等。
Using function references instead of a lambda expression like (a,b) -> a || b
is more in line with streams and lambda 'look-and-feel'.
Also, a method reference will produce less byte code, and thus mean faster execution times (a bit at least).
使用函数引用而不是 lambda 表达式(a,b) -> a || b
更符合流和 lambda 的“外观”。
此外,方法引用将产生更少的字节码,因此意味着更快的执行时间(至少有点)。
回答by Ashish Kumar
What's the difference between the following two cases.
boolean result = a || b; or Boolean result = Boolean.logicalOr(a,b);
以下两种情况有什么区别。
布尔结果 = a || 乙;或布尔结果 = Boolean.logicalOr(a,b);
I would like to put my points here regarding above question. Here is the body of Boolean.logicalOr
关于上述问题,我想在这里发表我的观点。这是身体Boolean.logicalOr
public static boolean logicalOr(boolean paramBoolean1, boolean paramBoolean2)
{
return (paramBoolean1) || (paramBoolean2);
}
So we can see that it's doing a || b
ultimately. But it becomes non short circuit when we use Boolean.logicalOr
instead of ||
. Because it (Boolean.logicalOr
) would be considered as (a || b)
which is different from a || b
when it comes with some other logical operators.
Example-: Please refer to the below code of snippet...
所以我们可以看到它a || b
最终正在做。但是当我们使用Boolean.logicalOr
而不是时它变成非短路||
。因为它(Boolean.logicalOr
)将被视为(a || b)
是从不同的a || b
时,它带有一些其他逻辑运算符。
示例-:请参考下面的代码片段...
public static void main(String[] args) {
boolean bCheck1 = false, bCheck2 = true, bCheck3 = false;
System.out.println("bCheck1\t" + "bCheck2\t" + "bCheck3\t" + "checkOR-Result\t" + "checkLogicalOr-Result");
bCheck1 = true; bCheck2 = true; bCheck3 = true;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = true; bCheck2 = true; bCheck3 = false;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = true; bCheck2 = false; bCheck3 = true;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = true; bCheck2 = false; bCheck3 = false;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = false; bCheck2 = true; bCheck3 = true;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = false; bCheck2 = true; bCheck3 = false;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = false; bCheck2 = false; bCheck3 = true;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
bCheck1 = false; bCheck2 = false; bCheck3 = true;
System.out.println(bCheck1 +"\t"+ bCheck2 +"\t"+ bCheck3 +"\t"+ checkOR(bCheck1, bCheck2, bCheck3) + "\t\t" + checkLogicalOr(bCheck1, bCheck2, bCheck3));
}
private static boolean checkOR(boolean bCheck1, boolean bCheck2, boolean bCheck3){
return bCheck1 && bCheck2 || bCheck3;
}
private static boolean checkLogicalOr(boolean bCheck1, boolean bCheck2, boolean bCheck3){
return bCheck1 && Boolean.logicalOr(bCheck2, bCheck3);
}
Below are the results-:
以下是结果-:
bCheck1 bCheck2 bCheck3 checkOR-Result checkLogicalOr-Result
true true true true true
true true false true true
true false true true true
true false false false false
false true true true false
false true false false false
false false true true false
false false true true false
We can see it's producing different results whenever it's been used with other logical operator. So one need to be cautious about using ||
over Boolean.logicalOr
or vice versa. Obviously Boolean.logicalOr
is more readable than ||
. But each one is having their significance and can be used as below.if(bCheck1 && bCheck2 || bCheck3)
can't be replaced by if(bCheck1 && Boolean.logicalOr(bCheck2, bCheck3))
However replacing if(bCheck1 && (bCheck2 || bCheck3))
to if(bCheck1 && Boolean.logicalOr(bCheck2, bCheck3))
would definitely be a good idea.
我们可以看到它在与其他逻辑运算符一起使用时会产生不同的结果。所以需要谨慎使用||
over Boolean.logicalOr
,反之亦然。显然Boolean.logicalOr
比||
. 但每一个都有其意义,可以如下使用。if(bCheck1 && bCheck2 || bCheck3)
不能被替换if(bCheck1 && Boolean.logicalOr(bCheck2, bCheck3))
但是替换if(bCheck1 && (bCheck2 || bCheck3))
到if(bCheck1 && Boolean.logicalOr(bCheck2, bCheck3))
绝对是一个好主意。