Java-Ternary Operator

Ternary Operator

The ternary operator is used to construct conditional expressions of the form :

Here, Expression1 can be any expression that evaluates to a boolean value. If Expression1 is true, then Expression2 is evaluated; otherwise, Expression3 is evaluated. The result of the ‘?’ operation is that of the expression evaluated. Both Expression2 and Expression3 are required to return the same type.

Program

1

Program Source

class Javaapp {

    public static void main(String[] args) {
        
        boolean con1 = 10<20 ? true : false;
        boolean con2 = 10>20 ? true : false;
        System.out.println("con1 = "+con1);
        System.out.println("con2 = "+con2);
    }
}

Leave a Comment