




- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


Java Ternary Operator - Conditional Logic in One Line
What is the Java Ternary Operator?
The ternary operator in Java is a shortcut for simple if else decisions. It lets you check a condition and choose between two values in one line. Use it when you only need to pick between two options, not for complex logic.
Syntax of the Ternary Operator
The ternary operator uses three operands—hence the name "ternary". Here's the basic structure:
condition ? expressionIfTrue : expressionIfFalse;
If the condition
is true, the value of the whole expression becomes expressionIfTrue
. Otherwise, it becomes expressionIfFalse
.
Simple Example for Ternary Operator
Here, the program checks if age
is greater than or equal to 18. If it is, it assigns "Eligible to vote" to the result
variable; otherwise, it assigns "Not eligible to vote". The result is then printed.
public class TernaryExample {
public static void main(String[] args) {
int age = 18;
String result = (age >= 18) ? "Eligible to vote" : "Not eligible to vote";
System.out.println(result);
}
}
Eligible to vote
Using Ternary with Numbers
In this example, we find the maximum of two numbers: a and b, using ternary operator.
We compare two numbers, a
and b
. If a
is greater than b
, the variable max
will get the value of a
; otherwise, it will get the value of b
.
public class TernaryExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);
}
}
Maximum: 20
Nested Ternary Operators
The ternary operator can also be nested to handle more than two conditions. This means placing one ternary operator inside another.
In this example, we check if the number is positive, negative, or zero. The first condition checks if num > 0
. If true, it returns "Positive". If not, it moves to the second condition: num < 0
. If that's true, it returns "Negative". Otherwise, it returns "Zero".
public class NestedTernaryExample {
public static void main(String[] args) {
int num = 0;
String type = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero";
System.out.println("The number is " + type);
}
}
The number is Zero
This reads like: if num > 0
, return "Positive", else if num < 0
, return "Negative", else return "Zero".
When to Use the Ternary Operator
The ternary operator shines in simple, straightforward decisions. It's especially handy when you want to assign a value based on a condition—without cluttering your code with verbose if-else blocks.
Avoid Overcomplicating
If your logic involves multiple conditions, calculations, or side effects (like printing or database calls), stick to traditional if-else
statements for clarity and maintainability.
Real-World Example - Discount Calculator
When calculating prices in an online store or billing system, it's common to offer different discounts to different types of customers. For instance, members might get a bigger discount than non-members. Instead of writing a full if-else block to apply the discount, we can simplify the logic using a ternary operator
If customer is already a member, that is isMember
is true
, the customer gets a 15% discount. If not, they get only 5%. The ternary operator helps apply the correct discount and calculate the final price in a single line of code.
public class DiscountCalculator {
public static void main(String[] args) {
boolean isMember = true;
double price = 250.0;
double discount = isMember ? 0.15 : 0.05;
double finalPrice = price - (price * discount);
System.out.println("Final Price: " + finalPrice);
}
}
Final Price: 212.5
Here, the discount is applied based on membership status. Simple and clean.
QUIZ
Question 1:What is the correct syntax of the ternary operator in Java?
Question 2:The ternary operator can completely replace all if-else statements in Java.
Question 3:What will the following code output?
int a = 5, b = 10;
int result = (a > b) ? a : b;
System.out.println(result);
int a = 5, b = 10;
int result = (a > b) ? a : b;
System.out.println(result);
Question 4:Which of the following are appropriate use cases for the ternary operator?
Question 5:Consider the code below. What will be printed?
int num = 0;
String type = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero";
System.out.println("The number is " + type);
int num = 0;
String type = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero";
System.out.println("The number is " + type);