My Java Development learning journey------> Confusion Automatic type conversion of Java for three mesh operations

Source: Internet
Author: User

Today, I saw two questions, but I did it all wrong. This two-face test also deepens the understanding of the automatic type conversion of the three-mesh operation.

Topic 1. The following code outputs the result is ().

public class Test {public static void main (string[] args) {int a=5; System.out.println ("value is:" + ((a<5)? 10.9:9));}}


A. Compilation error b.10.9 C.9 D. None of the answers are correct.


I chose C on the off-the-go, thinking the subject was too simple. And the answer is:D. None of the above answers are correct. It turns out that I have been trapped in this topic.

Parse: Three-mesh operation expression ( expression1? Expression2:expression3), the expression in the subject:((a<5)? 10.9:9), the second expression is 10.9, the third expression to 9. This is the automatic type conversion of Java based on the precision of the operator. Because of 10.9, 9 will automatically become 9.0. So the real output of this problem is 9.0.


Topic 2. The output of the following code is ().

<span style= "FONT-SIZE:18PX;" >public class Test  {public static void main (string[] args) {char x= ' x '; int i=10; System.out.println (false?i:x);    System.out.println (false?10:x);  }} </span>


A. b.120 x c.x + D. None of the answers are right.

The answer is: a.120 x.

Analytical:

int i=10; I is a variable, so the first output x is promoted to int, and the int value of x is 120, so the first output is 120.

As for the second output, the Java programming specification mentions that when one of the last two expressions is a constant expression and the other is T, and the constant expression can be represented by T, the output is the T type. So, since 10 is a constant, it can be represented by a char. The output is a char type, so the output is x.

System.out.println (TRUE?100:X); The output of this sentence is: D. Because D is a char value corresponding to 100.



The following is an original version of Oracle's official text:

15.25.2. Numeric Conditional Expressions

Numeric conditional expressions is standalone expressions (§15.2).

The type of a numeric conditional expression is determined as follows:

  • If the second and third operands have the same same type, then that's the type of the conditional expression.

  • If one of the second and third operands is of the primitive type T, and the type of the the the the the the result of Applyi Ng Boxing Conversion (§5.1.7) to T, then the type of the conditional expression is T.

  • If one byte Byte short Short of the operands is of type or and the other are of type or, then the type of the conditional Expression is short .

  • if one of the operands is of Type t  where t  is byte ,  short , Or char , and the other operand is a constant expression (§15.28) of Type int  whose value was representable in type  t , then the type of the conditional expression Is t .

  • if one of the operands is of Type t , Where t  is byte ,  short , Or character , and the other operand is a Constant expression of Type int  whose value is representable in the type < Span class= "type" >u which is the result of applying unboxing conversion to t , then T He type of the conditional expression Is u .

  • Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression I s the promoted type of the second and third operands.

    Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

The translation comes from:

< Span style= "font-size:24px" if the second and third operands can be converted to numeric types, there are several things: the
operand, one of which is A byte or byte type, and the other is a short or short type, then the expression is the short type
otherwise, the binocular value is lifted (binary numeric Promotion) is used in the type of operand, the type of the conditional expression is the second and third operand of the promoted type. Note: When the binocular value is lifted, the unboxing conversion and value set conversion (value set conversion)




For more information about the trinocular operator (Conditional Operator ? : ), you can view the official documentation for the Oracle company at the following address: http://docs.oracle.com/javase/specs/ jls/se8/html/jls-15.html#jls-15.25


Introduction to Boxing Conversion See the official documentation for the Oracle company at the following address:

http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7


The following two articles also have a certain reference:

http://bbs.csdn.net/topics/390822163


Http://www.educity.cn/wenda/371183.html



==================================================================================================

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

==================================================================================================



My Java Development learning journey------> Confusion Automatic type conversion of Java for three mesh operations

Related Article

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.