Java 8 Boolean.logicalOr Method

Java added 8 new methods in the Boolean class.

Let me just talk about one of them.

public static boolean Boolean.logicalOr(boolean a , boolean b)

Now, my question is: why are they needed?

What is the difference between the following two cases.

boolean result = a || b; or Boolean result = Boolean.logicalOr(a,b);

What is so special about Boolean.logicalOr() , and when I should prefer one by one.

+56
java java-8
Jan 19 '17 at 8:08
source share
3 answers

Basically, these methods are available for your convenience and make the code more readable using the method links in lambdas / streams. Consider an example:

 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 readable, right?

As stated in a comment by Sotirios Delimanolis, you can also look at javadoc and follow @see BinaryOperator . Or look at the javadoc feature package description.

+76
Jan 19 '17 at 8:16
source share

This is due to method references. Similarly, you can use the || (logical or) also in lambdas.

So there are other new features like Objects.isNull etc.

Using function references instead of a lambda expression such as (a,b) -> a || b (a,b) -> a || b , more consistent with threads and lambda representation.
In addition, a method reference will produce less byte code and therefore means faster execution time (at least a bit).

+52
Jan 19 '17 at 8:11
source share

What is the difference between the following two cases.
boolean result = a || b; or Boolean result = Boolean.logicalOr (a, b);

I would like to comment on this subject. Here is the body of Boolean.logicalOr

  public static boolean logicalOr(boolean paramBoolean1, boolean paramBoolean2) { return (paramBoolean1) || (paramBoolean2); } 

So we see that he does a || b a || b ultimately. But it becomes short lived when we use Boolean.logicalOr instead of || . Since it ( Boolean.logicalOr ) will be considered (a || b) , which is different from a || b a || b when it comes with some other logical operators.
Example: Please refer to the code snippet below ...

 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); } 

The following 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 see that it produces different results whenever it is used with another logical operator. Therefore, you need to be careful in using || over Boolean.logicalOr or vice versa. Obviously, Boolean.logicalOr more readable than || . But each of them has its own meaning and can be used as shown below. if(bCheck1 && bCheck2 || bCheck3) cannot be replaced by if(bCheck1 && Boolean.logicalOr(bCheck2, bCheck3))
However, replacing if(bCheck1 && (bCheck2 || bCheck3)) with if(bCheck1 && Boolean.logicalOr(bCheck2, bCheck3)) would definitely be a good idea.

+3
Jan 25 '17 at 9:13
source share



All Articles