Java self-study notes Series 9: The & quot; humor & quot; of the ternary Operators;

Source: Internet
Author: User

 

First, let's look at the following small program:

 
 
  1. Int num = 13;
  2. If (num> 10 ){
  3. Char c = (char) (num-10 + 'A ');
  4. System. out. println ("I want to get a char:" + c );
  5. } Else {
  6. System. out. println (num );
  7. }
  8. /*
  9. Output result:
  10. I want to get a char: D
  11. */

The above 02 rows ~ Line 07 code can be simplified into a line of code, because the existence of the ternary operator, but compared to above, it is more readable, and the use of the ternary operator can show your programming ox cross.

The ternary operators in Java are expressed as follows:

[Condition Statement]? [Expression 1]: [expression 2]

The above code can be converted into the following form:

 
 
  1. // Code snippet 1
  2. System. out. println ("I want to get a char:" + (num> 10? Num-10 + 'A': num ));
  3. /*
  4. Or write the following form, which is clear
  5. */
  6. System. out. print ("I want to get a char :");
  7. System. out. println (num> 10? Num-10 + 'A': num );
  8. /*
  9. All the above results are output: 68
  10. */

It seems that the syntax has changed, and the result has also changed.

Make the following changes to the expression num-10 + 'A', and check the result as follows, also do not want, or d ascii code value:

 
 
  1. // Code snippet 2
  2. System. out. println (num> 10? (Char) (num-10 + 'A'): num );
  3. /*
  4. Output result: 68
  5. */

When the format in the output statement is changed again, the result is the expected character D.

 
 
  1. // Code snippet 3
  2. System. out. println (char) (num> 10? (Char) (num-10 + 'A'): num ));
  3. System. out. println (char) (num> 10? Num-10 + 'A': num ));
  4. /*
  5. Both are output characters: D
  6. */

Why? The Java Virtual Machine leaves you alone. The following is a process to explore this issue for your reference only. If you have any questions, please correct them in time.

1The println () implementation is a function overload, Which is output according to different types in the parameter list. For example, the preceding small example calls:

 
 
  1. Println (int x) // call of code snippet 1
  2. Println (int x) // call of code snippet 2
  3. Println (char x) // call code snippet 3

 2So the output results of code segment 1 and code segment 2 can only be explained from the perspective of "implicit transformation of Java operators to prevent type overflow. therefore, in the above two fragments, the results of the expressions are char and int, respectively. The Java compiler will of course implicitly convert the char with a "narrow" to a "wide" int. Next, segment 3 is undergoing explicit transformation, transforming the int of "wide" into a char of "narrow.

 3As stated in 2, how should we explain the following results? First look at the Code:

 
 
  1. Int num = 13;
  2. Char x = 'X ';
  3. System. out. println (num> 10? X: num); // the ASCII value of X: 88
  4. System. out. println (num> 10? Num: x); // The value of num: 13
  5. System. out. println (num> 10? X: 90); // the output value is X.
  6. System. out. println (num> 10? 90: x); // The output is the character corresponding to the ASCII code value 90: Z

Hmm? The above explanation cannot be found here. There are different compilation methods for constants and variables.

 4In my opinion, if one expression is of the byte, short, and char type, and the other is a constant expression of the int type, if the int value can be expressed by one of the byte, short, and char Types, the returned result is one of the three.

To sum up, we can summarize the following three points:

1. If expression 1 and expression 2 have the same type, it is the type of the conditional expression.

2. If one expression is of the byte, short, and char type, and the other is a constant expression of the int type, if the int value can be expressed by one of the byte, short, and char Types, the returned result is one of the three.

3. When expression types 1 and 2 are inconsistent, the final type can be converted explicitly and forcibly.

So far, if you have any good ideas, you are welcome to discuss them.

By the way, expression 1 and 2 can also be a function call. I will not repeat it here.

This article is from the "IvanTian" blog. For more information, contact the author!

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.