Conditional (Ternary) Operator example – Basic java

  • by

Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one-liner replacement for the if-then-else statements.

conditional ternary operator


condition ? expr1 : expr2


If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.


public class Hello {
    public static void main(String ar[]) {
        int a = 5;
        int b = 4 ;

        int max = a > b ? a : b;
        System.out.println("Maximum number is : " + max);


Maximum number is : 5

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.