Java question mark? Operator usage
* Java provides a special ternary operator that is often used to replace an if-then-else statement of a certain type.
This operator is? It seems a little confused at first
But once you have mastered it, use? Operators are convenient and efficient.
*? The general format of operators is as follows:
Expression1? Expression2: expression3
Expression1 is a Boolean expression. If expression1 is true, expression2 is evaluated;
Otherwise, expression3 is evaluated. Whole? The expression value is the value of the evaluated expression (expression2 or expression3.
Expression2 and expression3 are expressions of any type except void, and they must be of the same type.
String type = NULL = ""? "": "1 ";
Type = 1 in this example;
Because null = "" is false, the value of the following expression is 1,
The value of type is 1;
? The result of the expression must be a value (or a value obtained after an operation). It is not a simple operation. For example, the following operations are incorrect.
Int A = 1;
Int B = 1;
A = B? System. Out. println ("A = B correct"): system. Out. println ("A = B incorrect ");