"StackOverflow Good question" Java + = operator Essence

Source: Internet
Author: User

problemTo this day, I always thought: i + = j is equivalent to i = i + j; But suppose there is: int i = 5;long J = 8; i = i + j cannot compile, but i + = J can be compiled. This suggests that there is a difference between the two. Does this mean that i + = j, which is actually equivalent to i= (type of i) (i + j)?

Essence Answer:This question, in fact, has been answered in the official documentation.please look here. §15.26.2 Compound Assignment Operators. And then copy the official documentation. For compound assignment expressions, E1 op= E2 (such as i + = J;I-=J, etc.) are actually equivalent to E1 = (t) ((E1) op (E2)), where T is the type of the element E1. For example, the following code
 Shortx= 3;x+= 4.6;
Equals
 Shortx= 3;x= ( Short)(x+ 4.6);


StackOverflow Link Http://stackoverflow.com/questions/8710619/java-operator
Column Introduction: very like StackOverflow, can always find a solution to the problem of incurable diseases. Accidentally found that the site has a list of heat. So select some of the more hot questions, read the answers to each question, and then sort them out according to your own understanding. Therefore, these articles are not true translation, but in accordance with their own understanding of a number of additions and deletions, polishing, hoping to put the above discussion, more streamlined and effective sharing to everyone. if you want to reprint, please specify the original address Http://blog.csdn.net/lizeyang

"StackOverflow Good question" Java + = operator Essence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.