Java provides two boolean operators boolean AND and boolean OR which are not provided by the other language and these operators are known as Short-Circuit logical operators. These operators gets the result of an logical operator, evaluating the right hand operator only if required.
As per the Boolean Logical Table for a logical AND (
& ) operation, if the left hand (first) operand is false, then the result is false irrespective of the right hand (second) operand. When short-circuit AND (&& ) is used, if the first value is false, second value is not evaluated.
Similarly for short-circuit OR (|| ), if the first value is true, then second value is not evaluated.
These short-circuit operators will be useful when we want to control the evaluation of right hand operand.
if( denom !=0 && num / denom > 10 ) num / denom > 10 will be evaluated only when denom is not zero, thus preventing divide by zero ArithmeticException .
Short circuit AND CODE class ShortCircuitAnd OUTPUT e = 50 DESCRIPTION Since THINGS TO TRY
|