Home Java JDK 8 Features Spring Boot React JS Servlet Data JPA JDBC Microservices Hibernate

Condition Operator

The Java Conditional Operator selects one of two expressions for evaluation, which is based on the value of the first operands. It is also called ternary operator because it takes three arguments.

Syntax: expression1? expression2:expression3;

public class ConditionalOperator {

            public static void main(String[] args) {

                        int n1 = 5, n2 = 11, n3 = 7;

                        System.out.println("\n Largest Number: " + ((n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3)));

            }

}

Output:

Largest Number: 11