Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is short-circuiting in Java?

In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.

What is short-circuiting AND when is it useful?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

Does Java use short-circuiting?

Java's && and || operators use short circuit evaluation. Java's & and | operators also test for the "and" and "or" conditions, but these & and | operators don't do short circuit evaluation.

How do short circuited operators work?

Short-circuit is a tricky method for evaluating logical operators AND and OR. In this method, the whole expression can be evaluated to true or false without evaluating all sub expressions. While debugging it looks like below, In the above program both operands Condition1() and Condition2() are evaluated.


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.


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.


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