English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

+= and ++ Simple introduction to operator distinction

We know:

int i = 5;

long j = 7;

i = i + j cannot be compiled, but i += j can be compiled and run, and the result is i = 12.

This is because:

i += j is equivalent to i = (int)(i+j);

In summary, for compound assignment expressions, E1 op= E2 (such as i += j; i -= j etc., is equivalent to E1 = (T)((E1) op (E2)) where, T is E1The type of this element.

This issue has already been answered in the official documentation. Official documentation address §15.26.2 Compound Assignment Operators

The above is what the editor introduces to everyone+= and ++ A simple introduction to the difference between operators, hoping it will be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time. Here we also thank everyone for their support of the Yell Tutorial website!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like