JavaCore java

Ternary Operator in Java

What is the Ternary Operator?

  • A shorthand way of writing if-else in one line.

  • Uses the symbols: ?:

  • Syntax:

    condition ? value_if_true : value_if_false;

Example 1 – Without Ternary

int n = 4;
int result = 0;

if (n % 2 == 0)
    result = 10;
else
    result = 20;

System.out.println(result);   // Output: 10

Example 2 – With Ternary

int n = 4;
int result = 0;

result = (n % 2 == 0) ? 10 : 20;

System.out.println(result);   // Output: 10

How It Works?

  • If the condition is true → executes the value after ?.
  • If the condition is false → executes the value after :.

Example 3 – Find Maximum of Two Numbers

int a = 8, b = 5;
int max = (a > b) ? a : b;

System.out.println("Maximum: " + max); // Output: 8

Example 4 – Nested Ternary

int marks = 75;
String grade = (marks >= 90) ? "A" :
               (marks >= 75) ? "B" :
               (marks >= 50) ? "C" : "Fail";

System.out.println("Grade: " + grade); // Output: B

Key Notes:

  • Ternary operator makes code short and cleaner.
  • But avoid making it too complex (nested ternary) → reduces readability.