My Java development and learning journey ------> solve the problem of automatic type conversion when Java performs three-object operations ------ java
Today, I saw two interview questions, and I did everything wrong. Through these two interview questions, we will also deepen our understanding of automatic type conversion for the three-object operation.
Question 1. The output result of the following code 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 above answers are correct.
I chose C without thinking about it. I think this question is too simple. The answer is: D. None of the above answers are correct. It turns out that I have caught the trap of this question.
Resolution: the three-object operation expression (expression1? Expression2: expression3), that is, the expression in this question :( (a <5 )? 10.9: 9), the second expression is 10.9, and the third expression is 9. This is why Java automatically converts data types based on the precision of operators. Because of 10.9, 9 will automatically change to 9.0. Therefore, the real output of this question is 9.0.
Question 2. The output result 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. 120 x B .120 120 C. x 120 D. None of the above answers are correct.
The answer is A.120 x.
Resolution:
I in int I = 10; is a variable. Therefore, the first output x is promoted to the int type, and the int value of x is 120, so the first output is 120.
As for the second output, Java programming specification mentioned: when one of the last two expressions is a constant expression and the other is a T type, while the constant expression can be expressed by T, the output result is of the T type. Therefore, because 10 is a constant, it can be represented by char. The output result is char type, so the output is x.
System. out. println (true? 100: x); the output result of this sentence is: d. Because d is the char value corresponding to 100.
The following is an official article in Oracle:
15.25.2. Numeric Conditional Expressions
Numeric conditional expressions are standalone expressions (§ 15. 2 ).
The type of a numeric conditional expression is determined as follows:
If the second and third operands have the same type, then that is the type of the conditional expression.
If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§ 5. 1.7) to T, then the type of the conditional expression is T.
If one of the operands is of typebyte
OrByte
And the other is of typeshort
OrShort
, Then the type of the conditional expression isshort
.
If one of the operands is of type T where T isbyte
,short
, Orchar
, And the other operand is a constant expression (§ 15. 28) of typeint
Whose value is representable in type T, then the type of the conditional expression is T.
If one of the operands is of type T, where T isByte
,Short
, OrCharacter
, And the other operand is a constant expression of typeint
Whose value is representable in the type Uwhich is the result of applying unboxing conversion to T, then the 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 is 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 is:
If the second and third operands can be converted to the numeric type, the following situations may occur:
If one of the operands is of the byte or Byte type and the other is of the short or Short type, the expression is of the short type.
One of the operands is T (T can be byte, short, or char), and the other is a constant of the int type. When it can be expressed as T, the expression is of the T type.
One of the operands is of the Byte type, and the other is a constant of the int type, which can be represented by the byte type. Therefore, this expression is of the byte type.
One of the operands is the Short type, and the other is the constant of the int type, which can be represented by the short type. Then this expression is the short type.
One of the operands is the Character type, and the other is the constant of the int type, which can be represented by the char type. Then this expression is the char type.
Otherwise, binary numeric promotion is used in the type of the operand. the type of the conditional expression is the type after the second and third operands are upgraded. Note: value set conversion)
About