Java – difference between S = s and S = s

I did a little test to manipulate a short film, and I encountered a compilation problem

short s = 1;
s += s;

And this one doesn't:

short s = 1;
s = s + s; //Cannot convert from int to short

I've read that SMS is automatically promoted to int, but what's the difference between the two codes?

Solution

You're right, short promotion to ints This occurs during the evaluation of binary operators, which is called binary number upgrade

However, compound assignment operators (such as =) can effectively clear this Section 15.26. 2 of the JLS status:

In other words, this is equivalent to a reduction

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>