Wednesday, January 30, 2019

Conditional Operators in Java

The conditional operators in Java- Conditional-AND (&&) and Conditional-OR (||) perform operations on two boolean expressions. Result is also a boolean value of true or false based on whether condition is satisfied or not.

How does Java Conditional operator work

Conditional-AND– If any of the two boolean expressions is false then the result is false.

As example- If in condition (A && B), expression A evaluates to false then result is false even if expression B evaluates to true. Let’s make it clear with a table.

A B A&&B
FalseFalseFalse
TrueFalseFalse
TrueTrueTrue
TrueFalseFalse

Conditional-OR- If any of the two boolean expressions is true then the result is true.

As example- If in condition (A || B), expression A evaluates to true then result is true even if expression B evaluates to false. Let’s make it clear with a table.

A B A||B
FalseFalseFalse
TrueFalseTrue
TrueTrueTrue
TrueFalseTrue

Java conditional operator example

public class ConditionalDemo {

    public static void main(String[] args) {
        int a = 7;
        int b = 8;
        int c = 5;
        
        // This condition evaluates to true
        if((a > c) && (b > c)){
            System.out.println("a and b both are greater than c");
        }
        
        // This condition evaluates to false
        if((a < c) && (b > c)){
            System.out.println("a and b both are greater than c");
        }
        
        // This condition evaluates to true
        if((a < c) || (b > c)){
            System.out.println("OR Condition (a < c) OR (b > c) ");
        }
        
        // This condition evaluates to true
        if(((a > c) && (b > c)) || (c < 3)){
            System.out.println("Both AND and OR used - First 
            expression (a > c) && (b > c) is true so OR 
            condition is true ");
        }
    }
}

Output

a and b both are greater than c
OR Condition (a < c) OR (b > c) 
Both AND and OR used - First expression (a > c) && (b > c) is 
true so OR condition is true 

Short-circuiting behavior of Java conditional operators

These conditional operators (&&, ||) exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

As explained above Conditional-AND evaluates to false if any of the two expressions is false. In that case if first expression evaluates to false there is no need to evaluate the second expression as result is going to be false anyway.

Same way for Conditional-OR if the first expression evaluates to true there is no need to evaluate the second expression because result is going to be true anyway.

Short-circuiting behavior Java example

Let us try to understand this short circuiting behavior with few examples.

In this example class there are two methods getCompValue1() and getCompValue2() and a conditional expression if(getCompValue1(7) && getCompValue2(5)) which is evaluated.

public class SCDemo {

    public static void main(String[] args) {
        if(getCompValue1(7) && getCompValue2(5)){
            System.out.println("Conditional expression evaluates to true");
        }else{
            System.out.println("Conditional expression evaluates to false");
        }

    }
    
    static boolean getCompValue1(int num){
        System.out.println("In getCompValue1 with value " + num);
        return num > 6;
    }
    
    static boolean getCompValue2(int num){
        System.out.println("In getCompValue2 with value " + num);
        return num > 6;
    }
}

Output

In getCompValue1 with value 7
In getCompValue2 with value 5
Conditional expression evaluates to false

Here note that return value of method getCompValue1() is true (7 > 6). That is why second expression (getCompValue2(5)) is also evaluated which evaluates to false (5 > 6). Thus the conditional expression evaluates to false.

Now if expression is changed in the getCompValue1 in such a way that this method returns false then notice what happens.

Class with getCompValue1() method changed

public class SCDemo {

    public static void main(String[] args) {
        if(getCompValue1(7) && getCompValue2(5)){
            System.out.println("Conditional expression evaluates to true");
        }else{
            System.out.println("Conditional expression evaluates to false");
        }

    }
    
    static boolean getCompValue1(int num){
        System.out.println("In getCompValue1 with value " + num);
        return num > 8;
    }
    
    static boolean getCompValue2(int num){
        System.out.println("In getCompValue2 with value " + num);
        return num > 6;
    }
}

Output

In getCompValue1 with value 7
Conditional expression evaluates to false

Now see the output, second expression is not even evaluated, where getCompValue2() method is called. Because first expression itself is false now (7 > 8). That’s what is "short-circuiting" behavior, where second operand is evaluated only if needed.

Another Short-circuiting example

Let us see another example, many times it happens that we check for null or zero and we go on to evaluate expression only if value is not zero or null.

As example

 
if(val1 != 0 && (val2/val1 > 5))

Here first part of the condition verifies if val1’s value is zero or not. If val1's value is zero expression val1 != 0 itself becomes false so the second expression is not evaluated. That saves you from a run-time exception in case val1 is zero.

That's all for this topic Conditional Operators in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Arithmetic and Unary Operators in Java
  2. Equality and Relational Operators in Java
  3. Ternary operator in Java
  4. instanceof Operator in Java
  5. Switch-Case statement in Java

You may also like-

  1. BigDecimal in Java
  2. StringBuilder in Java
  3. try-catch block
  4. Type Casting in Java
  5. Access modifiers in Java
  6. How HashSet works internally in Java
  7. Callable and Future in Java concurrency
  8. What is Dependency Injection in Spring