Java 8 Boolean.logicalOr method
In Java 8 new methods in Boolean
class have been added.
Let's just talk about one of them
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);
What's so special about Boolean.logicalOr()
and when should I prefer one over the other.
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:
Stream.of(/* .. some objects .. */)
.map(/* some function that returns a boolean */)
.reduce(Boolean::logicalOr);
trying to write this with 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.
It has to do with method references. Like this you can use the ||
(logical or) operator also in lambdas.
In this manner there also other new functions like Objects.isNull
etc.
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).
What's the difference between the following two cases.
boolean result = a || b; or Boolean result = Boolean.logicalOr(a,b);
I would like to put my points here regarding above question. Here is the body of 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...
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.