Short Circuit Logical Operators in Java with Examples
Last Updated :
02 Dec, 2021
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. Short circuit evaluation avoids unnecessary work and leads to efficient processing.
Below are the various types of Short circuits that occur in Java:
1. AND(&&) short circuit:
In the case of AND, the expression is evaluated until we get one false result because the result will always be false, independent of the further conditions. If there is an expression with &&(logical AND), and the first operand itself is false, then a short circuit occurs, the further expression is not evaluated, and false is returned.
Example: Short-circuiting using AND(&&) operator.
Java
// Java code to demonstrate the
// short circuiting using &&
import java.io.*;
class ShortCirAND {
public static void main(String arg[])
{
// Since first operand is false
// and operator is &&,
// Evaluation stops and
// false is returned.
if (false && true && true) {
System.out.println("This output "
+ "will not "
+ "be printed");
}
else {
System.out.println("This output "
+ "got printed actually, "
+ " due to short circuit");
}
// Whole expression will be evaluated,
// as no false is encountered
// before last condition
// Therefore no Short circuit
if (true && true && true) {
System.out.println("This output "
+ "gets print"
+ " as there will be"
+ " no Short circuit");
}
else {
System.out.println("This output "
+ "will not "
+ "be printed");
}
}
}
OutputThis output got printed actually, due to short circuit
This output gets print as there will be no Short circuit
2. OR(||) short circuit:
In the case of OR, the expression is evaluated until we get one true result because the result will always be true, independent of the further conditions. If there is an expression with ||(logical OR), and the first operand itself is true, a short circuit occurs, evaluation stops, and true is returned.
Example: Short-circuiting using OR( || ).
Java
// Java program to demonstrate the
// short circuiting using OR
class ShortCirOR {
public static void main(String arg[])
{
// Since first operand is true
// and operator is ||,
// Evaluation stops and
// true is returned.
if (true || false || false) {
System.out.println("This output "
+ "got printed actually, "
+ " due to short circuit");
}
else {
System.out.println("This output "
+ "will not "
+ "be printed");
}
// Whole expression will be evaluated,
// as no true is encountered
// before last condition
// Therefore no Short circuit
if (false || false || true) {
System.out.println("This output "
+ "gets print"
+ " as there will be"
+ " no Short circuit");
}
else {
System.out.println("This output "
+ "will not "
+ "be printed");
}
}
}
OutputThis output got printed actually, due to short circuit
This output gets print as there will be no Short circuit
Similar Reads
Java Logical Operators with Examples Logical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
8 min read
& Operator in Java with Examples The & operator in Java has two definite functions: As a Relational Operator: & is used as a relational operator to check a conditional statement just like && operator. Both even give the same result, i.e. true if all conditions are true, false if any one condition is false. However,
2 min read
Java Arithmetic Operators with Examples Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Short equals() method in Java with Examples The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method. Syntax ShortObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the insta
2 min read
&& operator in Java with Examples && is a type of Logical Operator and is read as "AND AND" or "Logical AND". This operator is used to perform "logical AND" operation, i.e. the function similar to AND gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.
1 min read
Java Relational Operators with Examples Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
10 min read