Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one-liner replacement for the if-then-else statements.
Syntax
condition ? expr1 : expr2
Description
If
condition
istrue
, the operator returns the value ofexpr1
; otherwise, it returns the value ofexpr2
.
Code
public class Hello { public static void main(String ar[]) { int a = 5; int b = 4 ; int max = a > b ? a : b; System.out.println("Maximum number is : " + max); } }
Output
Maximum number is : 5