The Java Ternary Operator is also called a Conditional Operator. The Ternary operator is mostly used in the decision-making process to return true or false. And it returns the statement depending upon the given expression result.
Java Ternary Operator Syntax
The basic syntax of a Conditional or Ternary Operator in this Programming is as shown below:
Test_expression ? statement1: statement2
If the given test condition or boolean expression evaluates to true, the conditional operator will return statement1. If the condition evaluates to false, then statement 2 is returned.
Java Ternary Operator Example
In this Program, we will use the Ternary or Conditional Operator to determine whether the person is eligible to vote.
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args) {
int age;
sc = new Scanner(System.in);
System.out.println(" Please Enter Your Age: ");
age = sc.nextInt();
String Message = (age >= 18)? " You are eligible to Vote " :
" You are Not eligible to Vote ";
System.out.println(Message);
/** REPLACE THE ABOVE CODE WITH FOLLOWING CODE
* System.out.println((age >= 18)?
* " You are eligible to Vote":
* "You are Not eligible to Vote");
*/
}
}
This Java Ternary Conditional Operator program allows the user to enter the age and assign the user entered integer value to the age variable. If the user enters a value is 18 or above, it will assign the first statement after the ? symbol to String variable Message. ” You are eligible to Vote.”
If the user enters below 18, the second statement (after the : symbol) will be assigned to the String variable Message. ” You are not eligible to Vote.”
Finally, we use the System.out.println statement to print the string data inside the Message variable.
Please Enter Your Age:
15
You are Not eligible to Vote
Let us try with a different value
Please Enter Your Age:
29
You are eligible to Vote
TIP: We can call the ternary operator a shorter version of the if-else statement. We use relational operators inside the conditional operator to evaluate the expression. Please refer to the Java if-else statement and Java relational operators from the Java tutorial page.
Java Nested Ternary or Conditional Operator Example
In this Program, we are going to use the Nested Ternary to find whether the person is eligible to work or not.
- In this ternary operator example, We declared a string variable called Message, and we assigned this variable to the conditional functionality.
- The first condition checks whether the age is less than 18. If this condition is True, will it return the first value after the ? symbol, which is You are too Young to Work
- When the first condition fails, it will execute the variable after : symbol. By using the Nested conditional operator, we check one more condition here (age >= 18 && age <= 60). If this condition is True, it will return the first value after the ? symbol, which is You are eligible to Work
- If the Nested condition of a conditional fails, the ternary operator will execute the variable after : symbol, which is You are too Old to Work.
NOTE: If the number of conditions (nested levels) is more, use the Java else if statement or the Java switch case because it may lead to code errors.
import java.util.Scanner;
public class ConditionalOperator {
private static Scanner sc;
public static void main(String[] args) {
int age;
sc = new Scanner(System.in);
System.out.println(" Please Enter Your Age: ");
age = sc.nextInt();
String Message = (age < 18)? " You are too Young to Work " :
(age >= 18 && age <= 60)? " You are eligible to Work ":
" You are too Old to Work ";
System.out.println(Message);
}
}

Java Output 2
Please Enter Your Age:
28
You are eligible to Work
Please Enter Your Age:
65
You are too Old to Work
Java Ternary Operator Multiple Conditions
In the following example, we use multiple conditions in a ternary operator expression. Based on the expression result, the conditional operator returns the result. Here, we used the Java logical operators to combine multiple conditions in a ternary operator. To compare the month, we use the Java String equals method.
We declared the day number as 15 and the month as August. The ternary operator has three expressions, and each expression has two conditions.
- day == 1 && month.equals(“January”) checks whether the given date is January 1st.
- day == 26 && month.equals(“January”) checks whether the given date is January 26th.
- day == 15 && month.equals(“August”) checks whether the given date is August 15th.
- day == 25 && month.equals(“December”) checks whether the given date is December 25th.
If any one of the expressions (multiple conditions) is True, it’s a national holiday. Otherwise, it’s a working day.
public class example {
public static void main(String[] args) {
int day = 15;
String month = "August";
String str = ((day == 1 && month.equals("January")) || (day == 26 && month.equals("January"))
|| (day == 15 && month.equals("August")) || (day == 25 && month.equals("December"))) ? "Holiday"
: "Working Day";
System.out.println(str);
}
}
Holiday
Java Ternary Operator vs if else
Both the ternary operator and if-else conditions help evaluate an expression and execute a statement when True or false. We can call the ternary operator a shorthand version of if-else.
If Else Example
We can use the if else statement to check a condition and execute multiple statements based on the expression result. So, we can use it for more complex operations where we may perform more calculations inside a true or false block. For example, print more than 1 statement inside the true block.
public class example {
public static void main(String[] args) {
int a = 10;
if (a > 0) {
System.out.println("Its a Positive Number");
} else {
System.out.println("Its a Negative Number");
}
}
}
Its a Positive Number
Java Ternary Operator Example
If the task is to choose between two values, the ternary operator is the easiest solution because of its one-line code nature and is easy to read.
public class example {
public static void main(String[] args) {
int a = -10;
String result = (a > 0) ? "Its a Positive Number" :
"Its a Negative Number";
System.out.println(result);
}
}
Its a Negative Number
Java Ternary Operator Null Check
When performing a string operation, most built-in string functions won’t allow null values. So, it is advisable to check whether the given string is null or not. To do so, we use the ternary operator.
In the following example, we use the ternary operator to check whether the username is null. If not, print the username; otherwise, print Guest.
public class example {
public static void main(String[] args) {
String username = null;
String result = username != null ? username : "Guest";
System.out.println(result);
}
}
Guest