July 16, 2022

Java Conditional Operators With Examples

There are two conditional operators in Java- && (known as Conditional-AND) and || (known as Conditional-OR). Both Conditional-AND (&&) and Conditional-OR (||) operators in Java perform operations on two boolean expressions and return a boolean value.

Conditional-AND– In case of && operator in Java if any of the two boolean expression is false then the result is false. Following expressions give an idea how conditional-AND operator works in Java.

true && true = true
true && false = false
false && true = false
false && false = false

Conditional-OR- In case of || operator in Java if any of the two boolean expression is true then the result is true. Following expressions give an idea how Conditional-OR operator works in Java.

true || true = true
true || false = true
false || true = true
false || false = false

Java example for conditional operators

public class ConditionalDemo {
  public static void main(String[] args) {
    int x = 3;
    int y = 4;
    int z = 5;
    //evaluates to false
    if((x > z) && (y > z)){
      System.out.println("x and y are greater than z");
    }
    //evaluates to true
    if((x < z) && (y < z)){
      System.out.println("x and y are less than z");
    }
		
    // One is true so evaluates to true
    if((x < z) || (y > z))
      System.out.println("Either x is less than z or y is greater than z");
  }
}
Output
x and y are less than z
Either x is less than z or y is greater than z

short-circuiting behavior with conditional operators

Conditional operators in Java exhibit short-circuiting behavior which means that the second operand in the expression is evaluated only if needed.

In case of && operator if any of the two boolean expression is false then the result is false. Which means if the first expression evaluates to false then there is no need to even evaluate the second expression as the result is anyway going to be false.

In case of || operator if any of the two boolean expression is true then the result is true. Which means if the first expression evaluates to true then there is no need to even evaluate the second expression as the result is anyway going to be true.

Java example exhibiting short-circuiting behavior

public class ConditionalDemo {
  public static void main(String[] args) {
    //evaluates to false
    if(evaluateMethod(4, 5) && evaluateMethod(5, 4)){
      System.out.println("evaluates to true");
    }else{
      System.out.println("evaluates to false");
    }
  }
  static boolean evaluateMethod(int num1, int num2){
    System.out.println("In evaluateMethod" );
    return num1 > num2;
  }  
}
Output
In evaluateMethod
evaluates to false

As you can see first expression itself evaluates to false that is why method is not even called in the second expression of the conditional-AND.

public class ConditionalDemo {
  public static void main(String[] args) {
    //evaluates to false
    if(evaluateMethod(7, 6) && evaluateMethod(5, 4)){
      System.out.println("evaluates to true");
    }else{
      System.out.println("evaluates to false");
    }
  }
  static boolean evaluateMethod(int num1, int num2){
    System.out.println("In evaluateMethod");
    return num1 > num2;
  }  
}
Output
In evaluateMethod
In evaluateMethod
evaluates to true

Now the first expression evaluates to true so the second expression is also evaluated, that is why method is called both of the times now.

To check for short-circuiting behavior in Conditional-OR we can change the expression to || in the above code.

public class ConditionalDemo {
  public static void main(String[] args) {
    //evaluates to false
    if(evaluateMethod(7, 6) || evaluateMethod(5, 4)){
      System.out.println("evaluates to true");
    }else{
      System.out.println("evaluates to false");
    }
  }
  static boolean evaluateMethod(int num1, int num2){
    System.out.println("In evaluateMethod");
    return num1 > num2;
  }  
}
Output
In evaluateMethod
evaluates to true

In the if condition, first expression evaluates to true, since its a conditional-or so no need to evaluate second expression as result is anyway going to be true. That is why evaluateMethod() in the second expression is not called.

That's all for the topic Java Conditional Operators With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment