What Is Ternary Operator in Java With Example - Simple Guide

Robert Jul 13, 2026

In Java, a ternary operator is a conditional operator that provides a concise way to perform operations based on a boolean condition. It's a short-hand way of writing an if-else statement, making your code more readable and efficient. The ternary operator takes three arguments: a condition, a value to return if the condition is true, and a value to return if the condition is false.

👉 Ternary Operator  👈
👉 Ternary Operator 👈

The syntax for the ternary operator is as follows: expression ? value_if_true : value_if_false. Here, 'expression' is the condition that evaluates to a boolean value (true or false), and 'value_if_true' and 'value_if_false' are the values that will be returned based on the condition.

10 Examples Of Ternary Operator In Java
10 Examples Of Ternary Operator In Java

Understanding the Ternary Operator

The beauty of the ternary operator lies in its simplicity and compactness. It allows you to test the result of an expression and, depending on whether it's true or false, make an immediate decision, making your code more straightforward and easier to understand.

What Is a Conditional (Ternary) Operator in JavaScript?
What Is a Conditional (Ternary) Operator in JavaScript?

Let's break down the syntax to understand how it works:

  • Expression: This is the condition that is evaluated first. It can be any expression that evaluates to a boolean value, such as 'x > y' or 'name.equals('John')'.
  • Value if true: This is the value that the expression will return if the condition is true. It can be any expression, such as 'x + y', 'name.toUpperCase()', or simply '10'.
  • Value if false: This is the value that the expression will return if the condition is false. Like the value if true, it can be any expression.
The Ternary Operator in JavaScript
The Ternary Operator in JavaScript

Ternary Operator Versus If-Else Statement

The ternary operator is often used as a shorthand for an if-else statement. Here's how you can rewrite a ternary operator as an if-else statement:

Ternary: age >= 21 ? "You can vote!" : "You cannot vote yet"

Java Operators Notes for Beginners ✨
Java Operators Notes for Beginners ✨

If-else:

if (age >= 21) {
    System.out.println("You can vote!");
} else {
    System.out.println("You cannot vote yet");
}

When to Use a Ternary Operator

The ternary operator is not always a good fit. It's best used when the decision to be made is simple and clear, and when the resulting expression can be used directly in the context in which it's written. Here are a few scenarios where using a ternary operator might be Appropriate:

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
  • Assigning a value to a variable based on a condition.
  • Returning a value from a method based on a condition.
  • Setting a variable based on a condition, for use immediately afterward.

Examples of Ternary Operator in Java

Operators in JAVA
Operators in JAVA
👉 Operators in java 👈
👉 Operators in java 👈
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
Iterator Design Pattern in Java
Iterator Design Pattern in Java
Types of Java Operators - Nourish Your Fundamentals - DataFlair
Types of Java Operators - Nourish Your Fundamentals - DataFlair
Relational Operators in Java beginner friendly and easy to learn 🤩
Relational Operators in Java beginner friendly and easy to learn 🤩
java array operations
java array operations
three types of operators in jawa, with the caption's description below
three types of operators in jawa, with the caption's description below
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
the cover of an ebook with text that reads, increment & decrement operators
the cover of an ebook with text that reads, increment & decrement operators
the cover for comparison operators true or false?
the cover for comparison operators true or false?
List of Java Operators with Examples | Complete Guide for Beginners
List of Java Operators with Examples | Complete Guide for Beginners
Java Cursor Enumeration vs Iterator vs ListIterator
Java Cursor Enumeration vs Iterator vs ListIterator
Java Relational Operators Explained Simply ☕💻 | Compare Values Like a Pro!
Java Relational Operators Explained Simply ☕💻 | Compare Values Like a Pro!
Java Programming Cheat Sheet
Java Programming Cheat Sheet
Python ternary operator cheat-sheet
Python ternary operator cheat-sheet
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Java Conditional Statements Cheat Sheet
Java Conditional Statements Cheat Sheet
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
Constructor in Java Explained | Default & Parameterized Constructor
Constructor in Java Explained | Default & Parameterized Constructor

Now that we understand the ternary operator's syntax and when to use it, let's look at some examples to solidify our understanding.

In the following examples, we'll use the ternary operator to set the value of a variable based on a condition:

Example 1: Determine Eligibility to Vote

The following code snippet uses the ternary operator to determine if a person can vote based on their age:

int age = 20;

String message = (age >= 21) ? "You can vote!" : "You cannot vote yet";

Example 2: Determine Discount Eligibility

In this example, we'll use the ternary operator to determine if a customer gets a senior discount based on their age:

int age = 65;

double price = (age >= 60) ? original_price * 0.9 : original_price;

Benefits and Limitations of Ternary Operator

While the ternary operator can make code more readable and concise, it's not always the best choice. Here are some benefits and limitations to consider:

  • Benefits:
    • Conciseness: The ternary operator allows you to write less code and make your code more readable.
    • Efficiency: The ternary operator is generally faster than an if-else statement because it's a single operation.

  • Limitations:
    • Complexity: While the ternary operator can make code more readable, it can also make it more difficult to understand, especially if it's used in complex expressions.
    • Stacked Ternaries: Using multiple nested ternary operators (stacked) can lead to complex, difficult-to-read code.

  • In conclusion, the ternary operator is a powerful and versatile tool in every Java programmer's toolbox. Used judiciously, it can make code more efficient and easier to understand. However, it's essential to understand its limitations and use it appropriately to avoid confusing readers or creating unreadable code.

    Practice is key to becoming proficient in using the ternary operator, so don't hesitate to experiment with it in your code. Happy coding!