编程语言
首页 > 编程语言> > An analysis of a simple Java basic interview question: short s1=1; s1 = s1 +1 will report an error?

An analysis of a simple Java basic interview question: short s1=1; s1 = s1 +1 will report an error?

作者:互联网

package common;
 
public class ShortTypeTest {
 
    /*   
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Short s1 = 1; s1 = (short) (s1 + 1);//simple type
                 Short s2=1; s2 +=1;//composite type, compound assignment operator +=,
        System.out.println(s1+";"+s2);
                 //What does the Console output output at runtime, do you know?
    }
 
}

 

Regarding a Java interview question, I will analyze it today without any problems, for beginners learning Java and unclear friends.

topic:

Short s1 = 1; s1 = s1 + 1; What's wrong? short s1 = 1; s1 += 1; What's wrong?

answer:
The Java specification has such rules

[

1. High position to low position requires forced conversion

2. The low position turns to the high position automatically.

]

Short s1 = 1; s1 = s1 + 1; What's wrong?

Answer: i is int type s1 short type s1 is automatically converted to int type after + operation. So wrong!

Short s1 = 1; s1 += 1; What's wrong?

A: If you think that the expression (x += i) is just a shorthand for the expression (x = x + i), this is not accurate.

Both expressions are called assignment expressions. (x = x + i) expressions use the simple assignment operator (=), while (x += i) expressions use the compound assignment operator. As mentioned in the Java language specification, compound assignment (E1 op=E2) is equivalent to simple assignment (E1=(T)((E1) op (E2)))), where T is the type of E1, unless E1 is only evaluated once. . In other words, the compound assignment expression automatically transforms the result of the computed calculation to the type of the variable on its left. If the type of the result is the same as the type of the variable, then this transformation will have no effect. However, if the result type is wider than the type of the variable, the compound assignment operator will silently perform a narrowed native type conversion.

Therefore, compound assignment expressions can be dangerous. To avoid this unpleasant surprise, don't apply the compound assignment operator to variables of type byte, short, or char. Because S1 is of the short type, it takes 2 bytes, and 1 is of the int type, which occupies 4 bytes. When the two types of values ​​are added, an automatic type promotion will occur, or the data will not fit. This is the reason._. That is to say, after s1+1, the result is int type, not short type, so you can think about it, put 4 bytes of things in two bytes of space, surely compile does not pass.

The latter one does not cause type promotion. The JAVA specification says that [e1+=e2 is actually e1=(T1)(e1+e2)], where T1 is the data type of e1. S1+=1 is equivalent to s1=(short)(s1+1), so it is correct.

标签:short,Java,assignment,s1,will,compound,type
来源: https://www.cnblogs.com/WLCYSYS/p/14177939.html