Today, a student is interviewing a. NET software engineer. The interview questions are as follows:
Short S = 0;
S = S + 1;
And
Short S = 0;
S + = 1;
What are the differences between the two expressions and what errors will be reported?
No, no, no. My first reaction is that the two statements seem to have no errors, and there is no difference.
The result was tested in vs2005. The first compilation error was reported, and the second was correct.
It's really strange, and it's strange for companies that have such questions.
My understanding:
S = S + 1; because 1 is int by default, S is short, and the result of "s + 1" is int by default, an error will be reported when the int result is assigned to short. If the precision is lost, it must be written as S = (short) (S + 1) to force conversion.
S + = 1; during compilation, the final value must be assigned to s, that is, the short type, so it will be automatically converted internally (I don't know which one has a better explanation)