What is short circuiting and how is it used when programming in Java? [duplicate]
Asked Answered
P

5

38

Possible Duplicate:
Does java evaluate remaining conditions after boolean result is known
Why do we usually use || not |, what is the difference?

I missed my class lecture the other day and I was wondering if anyone could give an explanation what short circuiting is and maybe an example of it being used in a simple Java program. Thanks for your help!

Pooka answered 18/2, 2012 at 21:37 Comment(1)
en.wikipedia.org/wiki/Short-circuit_evaluationKemeny
V
73

Short-circuiting is where an expression is stopped being evaluated as soon as its outcome is determined. So for instance:

if (a == b || c == d || e == f) {
    // Do something
}

If a == b is true, then c == d and e == f are never evaluated at all, because the expression's outcome has already been determined. if a == b is false, then c == d is evaluated; if it's true, then e == f is never evaluated. This may not seem to make any difference, but consider:

if (foo() || bar() || baz()) {
    // Do something
}

If foo() returns true, then bar and baz are never called, because the expression's outcome has already been determined. So if bar or baz has some other effect than just returning something (a side effect), those effects never occur.

One great example of short-circuiting relates to object references:

if (a != null && a.getFoo() != 42) {
    // Do something
}

a.getFoo() would normally throw a NullPointerException if a were null, but because the expression short-circuits, if a != null is false, the a.getFoo() part never happens, so we don't get an exception.

Note that not all expressions are short-circuited. The || and && operators are short-circuited, but | and & are not, nor are * or /; in fact most operators are not.

Vanzandt answered 18/2, 2012 at 21:44 Comment(0)
S
4

Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR) you can stop as soon as you find the first condition which satisfies or negates the expression.

For example, suppose you were evaluating a logical OR with several sub-expressions, each of which is very costly to evaluate:

if (costlyTest1() || costlyTest2() || costlyTest3()) { // ...

The JVM can stop evaluating the "costlyTest" functions as soon as it finds one that returns true, since that will immediately satisfy the boolean expression. So if costlyTest1() returns true then the other tests will be skipped. Similarly:

if (costlyTest1() && costlyTest2() && costlyTest3()) { // ...

The JVM can stop evaluating the functions as soon as it finds one that returns false, since that immediately negates the expression; so if costlyTest1() returns false then the other functions will not be called.

These rules pertain with any level of nesting of boolean expressions and can be taken advantage of to avoid unnecessary work, as demonstrated in the examples above.

Stinkpot answered 18/2, 2012 at 21:43 Comment(1)
Ah, I see. That was simple enough. Thanks for your quick and clear explanation!Pooka
U
3

Short Circuit: If the first part is true don't bother evaluating the rest of the expression. Same logic applies for false in the case of && which is also short circuiting

Undress answered 18/2, 2012 at 21:40 Comment(9)
More precisely: stop evaluating a logical expression as soon as the result is certain.Peccable
You just said that Java only evaluates the first part of an expression.Arletha
@Jeffrey:I don't understand your comment.Undress
@user384706 You said that if the first part of an expression is true, any expression, then it doesn't evaluate the rest of the expression. And that it does the same thing if the first part is false.Arletha
@Jeffrey:Yes.if(a && b) if a is false then the rest i.e. b is not evaluated.Likewise if(a || b) if a is true then the rest i.e. b is not evaluated.Where is the error?Undress
@user384706 You never specify which operations it short circuits on. A literal interpretation of your statement leads to a || b only ever evaluating a. (You say if the first part of any expression is true, then don't bother evaluating the rest of it.)Arletha
@Jeffrey:To be honest I still don't understand what is your complaint.What is the difference with the statement if (a == b || c == d || e == f) or if (costlyTest1() etc in the other answers that you didn't downvote?You want me to write some concrete expression in place of a && b?Really I don't followUndress
@user384706 Let's look at an example, shall we? a || b. If a is true, according to your answer, we don't bother evaluating the rest of the expression (okay, good, this makes sense). However, if a is false, we also don't bother evaluating the rest of the expression (but what if b is true?). According to your answer, this is exactly what the JVM does, when in fact, it doesn't.Arletha
@Jeffrey:Oh, this is what you mean.The false applies for && of course since this is also a short circuit operator.In the case of || if the first is false then it will try to evaluate the rest but this is of course common sense otherwise there wouldn't be a question on short circuit but how does AND and OR workUndress
J
0

Short-circuiting the evaluation of an expression means that only a part of the expression needs to be evaluated before finding its value. For example:

a == null || a.size() == 0

If a is null, the a.size() == 0 subexpression won't be evaluated, because the boolean operator || evaluates to true if one of its operands is true.

Similarly, for this expression:

a != null && a.size() > 0

If a is null, then the a.size() > 0 won't be evaluated, because the boolean operator && evaluates to false if one of its operands is false.

In the above examples the boolean operators && and || are said to be short-circuit, given the fact that the second operand might not be evaluated if the value of the first operand is enough to determine the value of the whole expression. For comparison, the & and | operands are the equivalent non-short-circuit boolean operators.

Junoesque answered 18/2, 2012 at 21:41 Comment(0)
W
0

Short circuiting is an alternative way of using the logical AND or OR operators (& or |)

e.g. a non short-circuit OR

if(false | true) {

}

The first condition and second condition are both evaluated even if false is not true (which it always is).

However it is was written as a short-circuit OR:

if(false || true) {

}

The first condition is only evaluated since it's false, true isn't evaluated since it's not required.

Weighty answered 18/2, 2012 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.