Talking about Java three mesh operation _java

Source: Internet
Author: User

The three-mesh condition operation formula is x?y:z where X's operation result is Boolean type, first calculates X's value, if true, then the entire three-mesh operation result is the expression Y's value, otherwise the entire operation result is the expression Z's value

Example: String s= "";

String x= "Default value";

S=s.isempty () x:s;

This code means: first to determine whether S is empty (the result is empty), and then execute s=x, that is, execute X

A little 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
   :d ay>a.day?1
   :d ay<a.day?-1:0;
 }

1, three-mesh operator (expression 1)? (expression 2):(expression 3), the calculation method is as follows: Expression 1 is a logical expression, and if its value is true, the value of the entire expression is the value of expression 2, otherwise the value of the 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 three-mesh operator from the right to the left of the binding, I was so divided
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 with the expression on the far right, and the result should be a value in 1, 0,-1.

The above is purely personal understanding. I don't know what you mean by this algorithm ....

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.