Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In Java 8 new methods in Boolean class have been added.

Let's just talk about one of them i.e

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

Now, my question is, Why are they added.

What's the difference between the two following 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.

share|improve this question
11  
What does the javadoc say? Does it have a @see reference that might be helpful? – Sotirios Delimanolis 17 hours ago
3  
Functionally, they are identical, but please do not write Boolean.logicalOr(a,b) in your code. When you have multiple, functionally identical ways to write code, you should always choose the most readable. – VGR 10 hours ago
up vote 30 down vote accepted

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).

share|improve this answer
1  
BiConsumer doesn't return any value. Why don't you use BinaryOperator like in the documentation? – shmosel 17 hours ago
8  
It might be worth noting that because it's a method call, you lose the short-circuit behavior of ||. – Kevin 15 hours ago
6  
@Kevin: that’s only relevant if you call it directly, which isn’t the intended use case anyway (as this answer explains). If you create a function from it, there is no difference between Boolean::logicalOr and (a,b)->a||b, as evaluating a function always implies evaluation of all arguments before executing the code. – Holger 15 hours ago
4  
a method reference will produce less byte code, and thus mean faster execution times Smaller .class files, maybe; but once the byte code is compiled to assembly by JIT, the original byte code being larger or smaller means nothing re: speed of execution. – walen 10 hours ago
3  
@walen True; one needs to benchmark to determine execution speed. Sebastian, I'd encourage you to do this. My findings are that the explicit lambda is slightly faster than the method reference, but that boxing overhead dominates this difference compared to a simple stream reduction that uses ints instead of Booleans. – Stuart Marks 7 hours ago

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.

share|improve this answer
1  
| can also be logic or, just not short-circuiting. – shmosel 17 hours ago
    
ok... added in the comment, that logicalOr is actually using || – Roland 17 hours ago
1  
I guess that's clearer. Though there isn't really any difference since it's operating directly on its arguments. – shmosel 17 hours ago
2  
well it lose some kind of type checking... :P if we accidentally changed it to integer, | will still work without complaining, while || will not pass compilation – Adrian Shum 16 hours ago
2  
IMO the second version is just as, if not more, readable. – Matti Virkkunen 12 hours ago

See, this is the code of

Boolean.logicalOr(boolean a , boolean b)

public static boolean logicalOr(boolean a, boolean b) {
        return a || b;
    }

Inside the method Boolean.logicalOr(*) logic is the same "a || b".

So, either you use directly method or write "a || b", there is no difference. It is given just for convenience and readability. There is no other technical reason behind it.

share|improve this answer
2  
Not entirely true, as using a static method reference instead of a lambda may generate different bytecode and may have (very minor) performance implications. – Boris the Spider 12 hours ago
    
@BoristheSpider: Is the Java compiler not smart enough to generate static methods from lambdas with no free variables..? – Matti Virkkunen 12 hours ago
1  
@MattiVirkkunen it most likely is, but that is not a requirement of the JLS as far as I'm aware. So it likely makes no difference, but there are no guarantees (again, as far as I know). – Boris the Spider 11 hours ago
    
“there is no difference” is completely wrong. More importantly than the other reasons mentioned so far, a || b is short-circuited; Boolean.logicalOr(a, b) isn’t. – Konrad Rudolph 10 hours ago
    
@KonradRudolph But as mentioned on Sebastian's answer, short-circuitng is moot if you wrap the operator in a lambda (a,b) -> a || b (the intended use-case for this function, but certainly not the only), since both expressions will already be evaluated before the call. – Kroltan 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.