The operator + = java implicit conversion cast (int and short type as an example)

short s1 = 1;

s1 = s1 + 1;

 

short s1 = 1;

s1 += 1;

 

The former is incorrect, the latter is correct.

For the former, because 1 is of type int, therefore s1 + 1 operation result is int type, we need to cast to type assigned to the short type.

The latter is correct translation, because s1 + = 1; corresponds to s1 = (short) (s1 + 1), casts implied

Guess you like

Origin www.cnblogs.com/diaohuluwa/p/11230664.html