In Java logical operators, if the evaluation of a logical expression exit 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 circuit that occur in Java:
- AND(&&) short circuit: In 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 first operand itself is false, then short circuit occurs, the further expression is not evaluated and false is returned.
Example: Short-circuiting using AND(&&) operator.
// Java code to demonstrate the// short circuiting using &&importjava.io.*;classShortCirAND {publicstaticvoidmain(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 circuitif(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");}}}chevron_rightfilter_noneOutput:This output got printed actually, due to short circuit This output gets print as there will be no Short circuit
- OR(||) short circuit: In 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 first operand itself is true, then a short circuit occurs, evaluation stops and false is returned.
Example: Short-circuiting using OR(||).
// Java program to demonstrate the// short circuiting using ORclassShortCirOR {publicstaticvoidmain(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 circuitif(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");}}}chevron_rightfilter_noneOutput:This output got printed actually, due to short circuit This output gets print as there will be no Short circuit
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | Shorts.indexOf(short[] array, short[] target) method with Examples
- Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
- Java.lang.Short toString() method in Java with Examples
- Short reverseBytes() in Java with Examples
- How to Convert a Short value to String value in Java with Examples
- Short compareTo() method in Java with Examples
- ShortBuffer put(int, short) method in Java With Examples
- How to Convert a String value to Short value in Java with Examples
- Short equals() method in Java with Examples
- Short hashCode() method in Java with Examples
- Short doubleValue() method in Java with Examples
- Java Guava | compare() method of Short Class with Examples
- Java.Lang.Short class in Java
- Short byteValue() method in Java with Example
- Short longValue() method in Java with Example
- Short floatValue() method in Java with Example
- Short compare() method in Java
- Short shortValue() Method in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.LongPredicate interface in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

