The ternary operator is a shortcut to assigning one of two values to a variable depending on a given condition.
It is a shortcut of the if-then-else statement.
public class Ternary {
public static void main(String[] args) {
boolean trueValue = true;
boolean factCheck_trueValue = trueValue ? true : false;
System.out.println("trueValue is " + factCheck_trueValue);
}
}
trueValue is true
boolean factCheck_trueValue = trueValue ? true : false;
- If trueValue is
true
, factCheck_trueValue define astrue
. - If trueValue is
false
, factCheck_trueValue define asfalse
.