In this tutorial you will learn about the Java Ternary Operator and its application with practical example.
Java Ternary Operator ( ? : )
In Java, a conditional or ternary operator is considered shorthand for a java if-else statement. The conditional operator is also called as “Ternary Operator”.
Syntax:
1 |
condition ? result1 : result2 |
If the condition is true the expression will return the result1, if it is not it will return result2.
Example:-
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { String result; System.out.println("W3Adda - Java Ternary Operator."); result = (10 > 15) ? "Greater" : "Smaller"; System.out.println(result); } } |
Output:-