In this unit, we will learn about Ternary Operator which is a one-liner replacement for if...else statements.
Ternary Operator is a conditional operator which works similar to if...else statements. When compared to if...else statements, the ternary operator consists of a shorter syntax.
condition ? expression1 : expression2; |
The ternary operator takes three operands (i.e, condition, expression1, and expression2). Here, the condition is evaluated and,
If the condition is true, expression1 is executed and the result is returned.
If the condition is false, expression2 is executed and the result is returned.
Example 1:
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int token = input.nextInt(); String message = (token == 20) ? "Collect Water Bottle" : "Invalid Token" ; System.out.println(message); input.close(); } } |
Here,
token == 20 is the condition.
"Collect Water Bottle" is expression1.
"Invalid Token" is expression2.
Let's understand the working of the ternary operator with different inputs.
Input & Output 1
Input & Output 2
Example 2:
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num1 = input.nextInt(); int num2 = input.nextInt(); int largest = num1 > num2 ? num1 : num2 ;
System.out.println(largest); input.close(); } } |
345 689
689
The above code prints the largest number between two given numbers. It isn't possible to replace every if...else statement with a ternary operator, but in some cases, it is a great tool to make our code more readable and shorter.
A Ternary operator can be used inside another ternary operator. It is called the nested ternary operator.
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int largest = (a >= b) ? ((a >= c) ? a : c) : ((b >= c) ? b : c);
System.out.println(largest + " is the largest among "+ a + ", " + b + ", and " + c); input.close(); } } |
In the outer ternary operator,
(a >= b) is the condition.
((a >= c) ? a : c) is the expression1.
((b >= c) ? b : c) is the expression2.
The code aims to find the largest number among the given three numbers.
Input & Output 1
Input & Output 2
Mistake 1: Return statements in expressions
In the ternary operator, putting return statements in the expressions is illegal. It always returns the result of the expression.
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); boolean isDivisibleByFive = (num % 5 == 0) ? return true : return false;
System.out.println(isDivisibleByFive); input.close(); } } |
55
file.java:7: error: illegal start of expression boolean isDivisibleByFive = (num % 5 == 0) ? return true : return false; ^ file.java:7: error: ';' expected boolean isDivisibleByFive = (num % 5 == 0) ? return true : return false; ^ 2 errors |
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); boolean isDivisibleByFive = (num % 5 == 0) ? true : false;
System.out.println(isDivisibleByFive); input.close(); } } |
55
true Mistake 2: Replacing colon (:) with a semicolon (;) |
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); boolean isDivisibleByFive = (num % 5 == 0) ? true ; false;
System.out.println(isDivisibleByFive); input.close(); } } |
245
file.java:7: error: : expected boolean isDivisibleByFive = (num % 5 == 0) ? true ; false; ^ file.java:7: error: illegal start of expression boolean isDivisibleByFive = (num % 5 == 0) ? true ; false; ^ file.java:7: error: not a statement boolean isDivisibleByFive = (num % 5 == 0) ? true ; false; ^ 3 errors |
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); boolean isDivisibleByFive = (num % 5 == 0) ? true : false;
System.out.println(isDivisibleByFive); input.close(); } } |
245
true
Ternary Operator is a one-liner replacement for if...else statements. It takes three operands (i.e, condition, expression1, and expression2).
The ternary operator evaluates the condition and executes a block of code based on the result of the condition.
A Ternary operator can be used inside another ternary operator. It is called the nested ternary operator.
The ternary operator always returns the value of the expression which is executed