Open In App

Short Circuit Logical Operators in Java with Examples

Last Updated : 02 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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");
        }
    }
}


Output

This 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");
        }
    }
}


Output

This output got printed actually,  due to short circuit
This output gets print as there will be no Short circuit


Similar Reads

Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
Shorts.indexOf(short[] array, short target) method of Guava's Shorts Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(short[] array, short target) Param
3 min read
Java Guava | Shorts.indexOf(short[] array, short[] target) method with Examples
Shorts.indexOf(short[] array, short[] target) method of Guava's Shorts Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(short[] array, short[] tar
3 min read
Java Logical Operators with Examples
Logical operators are used to perform logical "AND", "OR" and "NOT" operations, i.e. the function 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 consideration. One thing to keep in mind is, while using AND
10 min read
Java.lang.Short toString() method in Java with Examples
toString(short) The static toString() method of java.lang.Short returns a new String object representing the specified short. The radix is assumed to be 10.This is a static method hence no object of Short class is required for calling this method. Syntax: public static String toString(short b) Parameters: This method accepts a parameter b which is
2 min read
Short compareTo() method in Java with Examples
The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It returns an int value. It returns: 0 if 'otherShort' is e
2 min read
Short doubleValue() method in Java with Examples
The java.lang.Short.doubleValue() method of Short class is a built in method in Java which is used to return the value of the Short object as a double. Syntax: public double doubleValue() Return type: It return the value of ShortObject as double. Below is the implementation of doubleValue() method in Java: Example 1: // Java code to demonstrate //
2 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 instance of the Short object calling the equals method.
2 min read
Short hashCode() method in Java with Examples
The hashCode() method of Short class is a built in method in Java which is used to return the hash code of the ShortObject. Note: The hashCode() returns the same value as intValue(). Syntax ShortObject.hashCode() Return Value: It return an int value which represents the hashcode of the specified Short object. Below is the implementation of hashCode
2 min read
Short reverseBytes() in Java with Examples
The reverseBytes() method of Short class is a built in method in Java which is used to return the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value. Syntax : public static short reverseBytes(short a) Parameter: The method takes one parameter a of short type whose bytes are to be r
2 min read
Java Guava | compare() method of Short Class with Examples
Shorts.compare() method of Guava's Shorts Class is used to compare the two specified short values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative. Syntax: public static int compare(short x, short y) Parameters: This method ac
2 min read
ShortBuffer put(int, short) method in Java With Examples
The put() method of java.nio.ShortBuffer Class is used to write the given short into this buffer at the given index. Syntax: public abstract ShortBuffer put(int index, short s) Parameters: index:This parameter specifies to the index at which the short will be written.It is optional.s:This parameter specifies to the short value to be written Return
3 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 provide. Here are a few types: Arithmetic Operator
6 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 provide. Types of Operators: Arithmetic OperatorsU
10 min read
Java Assignment Operators with Examples
Operators constitute the basic building block of 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 provide. Types of Operators: Arithmetic OperatorsU
7 min read
Short shortValue() Method in Java
The shortValue() is an inbuilt method of the Short class in Java and is used to return the Short value of this value. Syntax: public short shortValue() Parameters: The method does not take any parameter. Return Value: The method returns the numeric value represented by this object after conversion to type short. Below programs illustrate the Short.
2 min read
Short compare() method in Java
The compare() method of Short class is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short. Syntax: public static int compare(short x, short y) Parameters: This method accepts two parameters: x: which is the first Short object to be compared. y: wh
2 min read
Short byteValue() method in Java with Example
The java.lang.Short.byteValue() method of Short class is a built in method in Java which is used to return the value of the Short object as a byte. Syntax ShortObject.byteValue() Return Value: It return the value of ShortObject as byte. Below is the implementation of byteValue() method in Java: Example 1: // Java code to demonstrate // Short byteVa
2 min read
Short floatValue() method in Java with Example
The java.lang.Short.floatValue() method of Short class is a built in method in Java which is used to return the value of the Short object as a float. Syntax: public float floatValue() Return type: It return the value of ShortObject as float. Below is the implementation of floatValue() method in Java: Example 1: // Java code to demonstrate // Short
2 min read
Short longValue() method in Java with Example
The java.lang.Short.longValue() method of Short class is a built in method in Java which is used to return the value of the Short object as a long. Syntax ShortObject.longValue() Return Value: It return the value of ShortObject as long. Below is the implementation of longValue() method in Java: Example 1: // Java code to demonstrate // Short longVa
2 min read
Difference Between byte, short, int and long Datatype in Java
There are two types of data types namely primitive datatype/fundamental and non-primitive/derived datatype. The primitive data type is defined as local sets or default set of datatype already present while deriving a datatype or creating a set of datatype using them are known as derived data types such as an array, stack, queue, tries, etc. Let's c
4 min read
Java.util.Bitset class | Logical operations
Bitset class also allows some of the logical operations on two Bitsets. The logical operations supported are : and, andNot, or, Xor. These are discussed in this article. 1. and(Bitset set) : This method performs a logical AND of this target bit set with the argument bit set and returns the values of 1st bitset also present in second bitset. Declara
5 min read
Java | Operators | Question 1
Predict the output of following Java Program class Test { public static void main(String args[]) { int x = -4; System.out.println(x>>1); int y = 4; System.out.println(y>>1); } } (A) Compiler Error: Operator >> cannot be applied to negative numbers (B) -2 2 (C) 2 2 (D) 0 2 Answer: (B) Explanation: See https://www.geeksforgeeks.org/
1 min read
Java | Operators | Question 2
Predict the output of following Java program. Assume that int is stored using 32 bits. class Test { public static void main(String args[]) { int x = -1; System.out.println(x>>>29); System.out.println(x>>>30); System.out.println(x>>>31); } } (A) 7 3 1 (B) 15 7 3 (C) 0 0 0 (D) 1 1 1 Answer: (A) Explanation: Please see https
1 min read
Java | Operators | Question 3
class Test { public static void main(String args[]) { System.out.println(10 + 20 + "GeeksQuiz"); System.out.println("GeeksQuiz" + 10 + 20); } } (A) 30GeeksQuiz GeeksQuiz30 (B) 1020GeeksQuiz GeeksQuiz1020 (C) 30GeeksQuiz GeeksQuiz1020 (D) 1020GeeksQuiz GeeksQuiz30 Answer: (C) Explanation: In the given expressions 10 + 20 + "Geeks
1 min read
Java | Operators | Question 4
class Test { public static void main(String args[]) { System.out.println(10*20 + "GeeksQuiz"); System.out.println("GeeksQuiz" + 10*20); } } (A) 10*20GeeksQuiz GeeksQuiz10*20 (B) 200GeeksQuiz GeeksQuiz200 (C) 200GeeksQuiz GeeksQuiz10*20 (D) 1020GeeksQuiz GeeksQuiz220 Answer: (B) Explanation: Precedence of * is more than +.Quiz of
1 min read
Java | Operators | Question 5
Which of the following is not an operator in Java? (A) instanceof (B) sizeof (C) new (D) >>>= Answer: (B) Explanation: There is no sizeof operator in Java. We generally don't need size of objects.Quiz of this Question
1 min read
Java | Operators | Question 6
class Base {} class Derived extends Base { public static void main(String args[]){ Base a = new Derived(); System.out.println(a instanceof Derived); } } (A) true (B) false Answer: (A) Explanation: The instanceof operator works even when the reference is of base class type.Quiz of this Question
1 min read
Java | Operators | Question 7
class Test { public static void main(String args[]) { String s1 = "geeksquiz"; String s2 = "geeksquiz"; System.out.println("s1 == s2 is:" + s1 == s2); } } (A) true (B) false (C) compiler error (D) throws an exception Answer: (B) Explanation: The output is “false” because in java + operator precedence is more than == op
1 min read
Java | Operators | Question 8
class demo { int a, b, c; demo(int a, int b, int c) { this.a = a; this.b = b; } demo() { a = b = c = 0; } demo operator+(const demo &obj) { demo object; object.a = this.a + obj.a; object.b = this.b + obj.b; object.c = this.c + obj.c; return object; } } class Test { public static void main(String[] args) { demo obj1 = new demo(1, 2, 3); demo obj
1 min read
Java | Operators | Question 9
Predict the output of the following program. Java Code class Test { boolean[] array = new boolean[3]; int count = 0; void set(boolean[] arr, int x) { arr[x] = true; count++; } void func() { if(array[0] && array[++count - 2] | array [count - 1]) count++; System.out.println("count = " + count); } public static void main(String[] args) { Test object =
1 min read
Article Tags :
Practice Tags :