The three-item conditional operation formula is x?y:z. The operation result of x is of boolean type. First calculate the value of x. If true, the result of the entire three-item operation is the value of expression y. Otherwise, the result of the entire operation is an expression. The value of formula z
Example: String s="";
String x="default";
s=s.isEmpty()?x:s;
The meaning of this code is: first determine whether s is empty (the result is empty), and then execute s=x, that is, execute x
Another more complicated
class Dates { int year,month,day; Dates(int x,int y,int z){ year=x; month=y; day=z; } public static int compare(Dates a){ return year>a.year ?1 :year<a.year?-1 :month>a.month?1 :month<a.month?-1 :day>a.day?1 :day<a.day?-1:0; }1. Three-item operator (expression 1)? (expression 2): (expression 3), the calculation method is as follows: Expression 1 is a logical expression. If its value is true, the entire expression is The value is the value of expression 2, otherwise it is the value of expression 3.
2. Example: int i = (5 > 3) ? (5 + 3) : (5 - 3); The result is i = 8. Because 5 > 3 is true, i = 5 + 3.
3. According to the combination of the three-point operator from right to left, I divided it like this
year > a.year ? 1 : (year < a.year ? -1 : (month > a.month ? 1 : (month < a.month ? -1 : (day > a.day ? 1 : (day < a .day ? -1 : 0)))));
So you should start from the expression on the rightmost, and the result should be a value in 1, 0, and -1.
The above is purely personal understanding. I didn't figure out what the meaning of your algorithm is. . . .
Please take some time to share the article with your friends or leave a comment. We will sincerely thank you for your support!