Does Java evaluate remaining conditions after boolean result is known?
Asked Answered
C

7

36

That is, if I have a statement that evaluates multiple conditions, in say a 'or' statement like so..

if(isVeryLikely() || isSomewhatLikely() || isHardlyLikely())
{
    ...
}

In the case that isVeryLikely() returns true at runtime, will isSomewhatLikely() and isHardlyLikely() execute? How about if instead of methods they were static booleans?

Canakin answered 15/6, 2011 at 1:50 Comment(1)
The statement in the spec: docs.oracle.com/javase/specs/jls/se14/html/…Maus
O
66

The || and && operators are short-circuiting.

true || willNeverExecute();
false && willNeverExecute();
Octosyllable answered 15/6, 2011 at 1:52 Comment(0)
S
19

The first thing that you need to note is that Java conditional statements can only take boolean, unlike other languages like C/C++ where any non-zero value will evaluate to true.

That being said, there are 2 types of operators, the first is known as the shor-circuit types:

&& and ||

while the other are the NON-short-circuit types:

& and |

For the Short-Circuit types, once a logical output can be found as an answer to the expression, the rest of the statement will be dumped. For the NON-Short-Circuit types, they will continue to evaluate all conditions.

With this in mind, what you have:

if(isVeryLikely() || isSomewhatLikely() || isHardlyLikely())
{
    ...
}

Java will:

  1. First check if isVeryLikely() returns true. If true, then it will not continue further.
  2. If isVeryLikely() returns false, then invoke isSomewhatLikely() to see if it returns true. If true, nothing else if evaluated. If false, goto 3.
  3. isHardlyLikely() is invoked. If true, entire expression is true, otherwise false, entire expression is false.

The entire concept you're asking about is "How does Java evaluate Logical Expressions." Good question :)

Hope it helps! Cheers!

Smelter answered 15/6, 2011 at 2:0 Comment(0)
F
3

No, java uses short-circuit evaluation on expressions using || and &&. See here for more info.

Fallacy answered 15/6, 2011 at 1:52 Comment(1)
Not all "boolean expressions"; you can use & and | with booleans, and they are not short-circuiting.Octosyllable
G
2

Because the || is short circuiting, the statement will be evaluated to true as soon as it hits the first true condition, regardless of whether or not the operands are static.

In short, no the two other functions will not be evaluated if the first returns true.

Goodrum answered 15/6, 2011 at 1:56 Comment(0)
C
1

The short answer is, it will evaluate until has enough to conclude whether it is T/F.

There is something called boolean short-circuiting. Essentially it will try and evaluate only what it needs to (if you use the && or || operators) and the leave. You can take advantage of this in a few ways:

(a() || b())
  1. If b() would throw an exception, if a() is true, then it would not even try and check the b(). A type of chain-checking mechanism.

  2. If the latter evaluations are resource consuming, you can move them to the end of the evaluation (eg. b() takes 10 times longer)

  3. If the most likely path can be determined by a certain clause, put them first. This would also speed up execution.

Cookshop answered 23/9, 2015 at 14:13 Comment(0)
H
1

As an update for Kotlin users, you can use 'or' operator in Kotlin std library in order to check all the expressions in the if statement and not performing a short-circuit.

In the expression presented in the initial question the code in Kotlin should be:

if(isVeryLikely() or isSomewhatLikely() or isHardlyLikely()) {
    ...
}
Hogshead answered 16/4, 2020 at 10:16 Comment(0)
G
0

Whether for boolean expression specified in the questions are static booleans, or they are methods returning a boolean value, in both the cases - expression evaluation will start from the left and conditions will be evaluated one-by-one. The first condition which gives a value true will short-circuit the evaluation. It will directly give the result as true with the remaining conditions not being evaluated. This is how ||(OR) works in Java using the concept of short-circuit evaluation.

If you want to understand more about short-circuits in general you can refer an article I have written on my blog - http://www.javabrahman.com/programming-principles/short-circuiting-or-short-circuits-in-boolean-evaluations-in-programming-and-java/

Geminian answered 4/11, 2015 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.