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.
toString(boolean b) returns a String object representing the specified boolean. If the specified boolean is true, then the string "true" will be returned, otherwise the string "false" will be returned.
String toString() : This method returns a String object representing this Boolean's value. If this object represents the value true, a string equal to “true” is returned. Otherwise, the string “false” is returned. int hashCode() : This method returns a hash code for this Boolean object.
boolean is a primitive type, and therefore can not be null.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With