Java three-object operator boolean? Type conversion in x: y

Source: Internet
Author: User

The following code is a question in the interview book:
[Java]
Int a = 5;
System. out. println ("a =" + (a <5 )? 10.9: 9 ));
The code output is 9.0. Because the three-object operator has a 10.9 value, java will automatically convert the type based on the precision type of the operator. So 9 will change to 9.0
[Java]
Char x = 'X ';
Int I = 10;
System. out. println (false? I: x );
System. out. println (false? 100: x );
The output of this Code is
120
X
For the first row of output, x is upgraded to the int type, which is the same as the previous example.
For the second row, 10 is a constant. If one of the two expressions in the three orders operator is a constant expression and the other is a type T expression, and the constant expression can be expressed by T, the output result is of the T type. Therefore, the output is a character x.
[Java]
System. out. println (true? 100: x );
Therefore, this line of code will output the char value of d, that is, 100.

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.