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.