Ternary Operator in Java

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 as true.
  • If trueValue is false, factCheck_trueValue define as false.

Leave a Reply

Your email address will not be published.

ANOTE.DEV