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?
The ||
and &&
operators are short-circuiting.
true || willNeverExecute();
false && willNeverExecute();
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:
- First check if isVeryLikely()
returns true. If true, then it will
not continue further.
- If isVeryLikely() returns false,
then invoke isSomewhatLikely() to
see if it returns true. If true,
nothing else if evaluated. If false,
goto 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!
No, java uses short-circuit evaluation on expressions using ||
and &&
. See here for more info.
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.
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())
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.
If the latter evaluations are resource consuming, you can move them to the end of the evaluation (eg. b() takes 10 times longer)
If the most likely path can be determined by a certain clause, put them first. This would also speed up execution.
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/