Ternary Operator

Ternary Operator

Ternary operator is like if-else statement. This is a one-liner replacement for the if-else statement.

Syntax

variable = condition ? expressionIfTrue: expressionIfFalse;

Example

public class Main {
    public static void main(String[] args) {
        int num1  = 10;
        int num2 = 20;

        int max = (num1 >num2) ? num1 : num2;
        
        System.out.println("The max number is "+max);
    }
}
Show Output