Java + operator Essence __java

Source: Internet
Author: User

Today in GitHub saw an open source project, which is in the collation and translation StackOverflow on the hot questions, the first question is in the discussion i = j is equal to I = i + j; The conclusion is not equal to. The argument is as follows:

if int i = 9;   Long j = 11; Then i = i + j cannot compile, but i = j can compile. The description i + j is actually equivalent to i= (type of i) (i + j);

Refer to Official document: http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2

Document content:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1 , except that E1 are evaluated only once.

For example, the following code is correct:

Short x = 3;
x + 4.6;

and results in X has the value 7 because it is equivalent to:

Short x = 3;
x = (short) (x + 4.6);
For a compound assignment expression, the E1 op= E2 (such as I = J, I-J, and so on) is actually equivalent to E1 = (t) (E1) op (E2), where T is the type of E1 element.

StackOverflow Link Http://stackoverflow.com/questions/8710619/java-operator




The original git address is: https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/java-operator.md
A C

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.