The ternary operator in Java is a shorthand way of writing simple if-else statements, which makes your code more concise and clean. This operator is also known as the conditional operator because it makes decisions based on a boolean condition.
In Java, the ternary operator is represented by the symbols (? :). This is the only conditional operator in Java that takes three operands.
- A condition (boolean expression).
- An expression/value if the condition is true.
- An expression/value if the condition is false.
Basic Syntax of Ternary Operator in Java
The general syntax of ternary operator in Java is as:
variable = (condition) ? expression1 : expression2;In the above syntax,
- condition: This is a boolean expression that evaluates to true or false.
- expression1: This represents the value assigned to the variable if the condition is true.
- expression2: This represents the value assigned to the variable if the condition is false.
The ternary operator evaluates a boolean expression and returns one of two values: one when the condition is true and another when the condition is false. Look at the below diagram to understand better.

Basic Example of Ternary Operator in Java
Let’s start with a simple program.
Example 1: Simple Number Comparison
public class TernaryBasic {
public static void main(String[] args) {
int a = 10;
int b = 20;
// Using ternary operator to find maximum number.
int max = (a > b) ? a : b;
System.out.println("Maximum value: " + max);
}
}Output:
Maximum value: 20In this example, the conditional expression (a > b) evaluates to false since 10 is not greater than 20. Therefore, the expression after the colon (b) is assigned to the variable max.
Example 2: Checking Even or Odd
public class EvenOddCheck {
public static void main(String[] args) {
int number = 15;
String result = (number % 2 == 0) ? "Even number." : "Odd number.";
System.out.println(number + " is " + result);
}
}Output:
15 is Odd number.In this example, the condition checks if the number is divisible by 2. Since 15 % 2 is equal to 1 (not zero), the condition is false. Therefore, “Odd” is assigned to the variable result.
Example 3: Replacing if-else with Ternary Operator
Traditional if-else code:
public class IfElseExample {
public static void main(String[] args) {
int age = 20;
String status;
if (age >= 18) {
status = "Eligible to Vote";
} else {
status = "Not Eligible to Vote";
}
}
}Using ternary operator:
public class ReplacingWithTernary {
public static void main(String[] args) {
int age = 20;
String status = (age >= 18) ? "Eligible to Vote." : "Not Eligible to Vote.";
System.out.println(status);
}
}Output:
Eligible to Vote.Nested Ternary Operator in Java
Java also allows you to nest ternary operators for multiple conditions. Let’s take an important example on it.
Example: Grade Assignment
public class GradeSystem {
public static void main(String[] args) {
int score = 85;
char grade = (score >= 90) ? 'A' :
(score >= 80) ? 'B' :
(score >= 70) ? 'C' :
(score >= 60) ? 'D' : 'F';
System.out.println("Score: " + score + ", Grade: " + grade);
}
}Output:
Score: 85, Grade: BIn this example, the first condition (score >= 90) evaluates to false and the second condition (score >= 80) evaluates to true. Therefore, the grade B is assigned to the variable grade.
Ternary Operator with Return Statement
You can directly return values using ternary operator inside method. Look at the example below.
Example:
public class TernaryExample3 {
public static String checkEligibility(int age) {
return (age >= 18) ? "Eligible" : "Not Eligible";
}
public static void main(String[] args) {
System.out.println(checkEligibility(16));
System.out.println(checkEligibility(21));
}
}Output:
Not Eligible
EligibleAdvanced Examples on Java Ternary Operators
Let’s take some important advanced example programs based on the ternary operator in Java. You must practice them to clear the concept.
Example 1: Ternary Operator with Object
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class TernaryWithObject {
public static void main(String[] args) {
int age = 25;
// Ternary operator is returning objects.
Person person = (age >= 18)
? new Person("John", 25)
: new Person("Tommy", 15);
System.out.println("Name: " + person.name + ", Age: " + person.age);
}
}Output:
Name: John, Age: 25As you see in the above example, the ternary operator can also return objects.
Example 2: Null Safety Check
public class NullSafety {
public static void main(String[] args) {
String name = null;
String displayName = (name != null) ? name : "Guest";
System.out.println("Welcome, " + displayName);
}
}Output:
Welcome, GuestExample 3: Ternary Operator with Multiple Conditions
public class MultipleConditions {
public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;
String canDrive = (age >= 18 && hasLicense) ? "You can drive the vehicle." : "You cannot drive the vehicle.";
System.out.println(canDrive);
}
}Output:
You can drive the vehicle.In this example, the condition (age >= 18 && hasLicense) evaluates to true, the message “You can drive the vehicle.” prints on the console.
Example 4: Method Calls with Ternary Operator
public class MethodTernary {
public static void main(String[] args) {
int x = 5;
int y = 10;
// Calling different methods based on condition.
int result = (x > y) ? multiply(x, y) : add(x, y);
System.out.println("Result: " + result);
}
static int multiply(int a, int b) {
return a * b;
}
static int add(int a, int b) {
return a + b;
}
}Output:
Result: 15When to Use Ternary Operators in Java?
You can use the ternary operator in Java programming for the following scenarios:
- If you have simple conditional assignments, you can use ternary operator instead of if-else statement.
- When you need to return different values based on conditions, use ternary operator.
- You can use ternary operator to assign a default value if a condition is not met. This is called default (fallback) value assignments.
When to Avoid Ternary Operators?
There are the following situations where you should not use ternary operator in Java program. They are:
- You should not use the ternary operator when the condition or expressions become too complicated. Instead, you use if-else statement, making the code more readable.
- When you need to execute multiple statements in each branch, you should not use ternary operator. Use if-else when multiple operations are required.
- You should not nest ternary operators deeply because they can reduce readability.
FAQs on Ternary Operator in Java
1. What is the difference between ternary operator and if-else statement in Java?
The if-else statement can execute multiple lines of code, while the ternary operator is an expression that returns a value in a single line.
2. Can ternary operator replace all if-else statements?
No, it’s best suited for simple conditional assignments, not for complex logic.
3. Can ternary operator return different data types?
No, both expressions must return compatible types.
4. Is ternary operator faster than if-else?
Performance difference is negligible, but ternary makes the code shorter.
Conclusion
The ternary operator in Java is a powerful and concise way to handle simple and straightforward conditional logic. It makes your code more concise and readable when used appropriately. I hope that you will have understood the basic syntax of ternary operator.





