Java interview questions about assignments

Interview questions:
(. 1) S = Short. 1;
S = S +. 1;
(2) S = Short. 1;
S = +. 1;
Q: The above two code has no problem, and if so, where the problem?

A: (1) The first is wrong, the error will be a loss of precision, because the short type s to participate in operation
to change an int, short type of the s + 1 = s assigned to the loss of precision will be left to be written
s = (short) (s + 1); fishes.
(2) This output 2, no problem. Why, because the extended assignment operator is actually implied
a cast.
s + = 1;
not equivalent to s = s + 1;
but is equivalent to s = (s data type) (s + 1);

 

Guess you like

Origin www.cnblogs.com/lszbk/p/12318624.html