Usage and type conversion of JAVA three-mesh operator Boolean?condition1:condition2 __java

Source: Internet
Author: User

Tri-Mesh operator < expression 1>?< expression 2>:< expression 3>; "?" The meaning of an operator is to first evaluate the value of expression 1 and, if true, execute Expression 2 and return the result of expression 2; If the value of expression 1 is false, the expression 3 is executed and the result of expression 3 is returned.

The above is the basic definition and use of the three-mesh operator. On the surface, it should be relatively simple. In the Book of Java Programmer interview, we have seen two interesting topics.

Topic 1: "China northeast famous software company D2009 March test"

int a=5;
System.out.println ("a=" + (a<5) (10.9:9));

A. Compilation errors

B. 10.9

C. 9

D. None of the above answers are correct

Perhaps like most people, just start to assume that A<5 is false, then the result is 9, choose c.

Think carefully, this is the problem set trap. In the expression = (a<5)? 10.9:9 There is a 10.9, this is the Java will be based on the operator's accuracy of automatic type conversion, because the front is 10.9, then the following 9 will then become 9.0!

Topic 2: "China Northeast famous software company D2009 March test"

Char x= ' x ';
int i=10;
System.out.println (FALSE?I:X);
System.out.println (FALSE?100:X);

A. x

B. 120 120

C. x 120

D. None of the above answers are correct

The answer is a.

Parsing: System.out.println (false?i:x) is the same as the last topic 1, X is promoted to int type, so output X's ASCII code
For the second row, because 100 is a constant expression. If the two expressions in the three-mesh operator are a constant expression and the other is an expression of type T (char in this case), and the constant expression can be represented by T, the output result is the type T. So the output is a character

Core ideas:

1. If one of the two expressions in the three-mesh operator is a constant expression and the other is an expression of type T, and a constant expression can be represented by T, the output result is the type T.

2. If all are constant expressions, use the UP type conversion

Enter the exercise mode:

(1)

Char x= ' x ';
int i=10;

System.out.println (FALSE?10000:X);

System output: X

Okay, what if it's modified?

Char x= ' x ';
int i=10;

System.out.println (FALSE?100000:X);

What is the output of the system? 120.

Parsing: "Core idea 1" 10000 can be represented by the T type of the subject, so the output is T type, output x, but 100000 is not, because it exceeds the ASCII range. Then we're going to output the ASCII value of X.

Continue to extend

Char x= ' x ';

System.out.println (TRUE?100:X);

What about this one.

Resolution: The same applies to "core idea 1", after true enter condition1.100 can be represented by T type, then input will be T type (char type), 100 corresponds to ASCII is D, then output d.

(2)

Char x= ' x ';

System.out.println (TRUE?X:10);

System.out.println (true?x:10.0);

OK, the first line should output is X, this is no problem. So the second line. Results: 120.0

Parsing: Apply "core idea 1", but 10.0 cannot be type T, then output will not be T type. 120 will be upgraded to double type to 120.0

(3)

System.out.println (fasle?9:10.0);

System.out.println (true?9:10.0);

Results: 10.0 9.0

Resolution: For Core idea 2, the result of the first line Fasle is the highest type, and the second line 9 will be upgraded to double to 9.0

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.