Two examples of C # + + operators

Source: Internet
Author: User

Just occasionally saw Justjavac write Java FAQ-Dora (i) and Java FAQ-Dora (ii). It mentions the two traps of the Java compound assignment operator: 1 The compound assignment operator has the semantics of coercion type conversion; 2) = = The left side must be a numeric type in the original type, or a string type.

jls3e says:

Java Language Specification, 3rd Edition wrote

15.26.2 Compound Assignment Operators

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.

The core of such a sentence, there are a lot of details behind the provisions, interested can go to the official website to read: jls3e:15.26.2 compound assignment Operators

Read it, only when the left-hand side of the compound assignment operator is an array, and the array element type is string, it refers to + = specifically for string, and in other parts of 15.26.2 there is no mention of Justjavac's second restriction, which is strange.

ECMA-334 says:

ECMA-334, 4th Edition wrote

14.14.2 Compound Assignment

An operation of the "form x op= y is processed by applying binary operator overload (resolution) as if" §14.2.4 N was written x op y. Then,

If The return type of the selected operator was implicitly convertible to the type of x, the operation is evaluated as X = x op y, except that X was evaluated only once.

Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is explicitly co Nvertible to the type of x, and if Y-implicitly convertible to the type of x or the operator is a shift operator, then The operation is evaluated as X = (T) (x op y), where T is the type of x, except this x is evaluated only once.

Otherwise, the compound assignment is invalid, and a compile-time error occurs.

The term "evaluated only once" means, the evaluation of x op y, the results of an any constituent expressions of X are Temporarily saved and then reused when performing the assignment to X. [Example:in the Assignment A () [B ()] + = C (), where A is a method returning int[], and B and C are methods returning int., the methods are invoked only once, in the order A, B, C. End example]

When the left operand of a compound assignment are a property access or indexer access, the property or indexer shall Both a get accessor and a set accessor. If It is not the case, a compile-time error occurs.

The second rule above permits x op= y to is evaluated as X = (T) (x op y) in certain contexts. The rule exists such this predefined operators can be used as compound operators then the left operand is of type sbyt E, byte, short, ushort, or char. Even when both arguments are of one of those types, the predefined operators-produce a result of type int, as described in §14.2.6.2. Thus, without a cast it would is possible to assign the "to" left operand.

The intuitive effect of the "rule" for predefined operators are simply that x op= y are permitted if both of x op y and x = y are permitted. [Example:in The following code

C # code

byte B = 0;
char ch = ' n ';
int i = 0;
B + 1;    Ok
B + 1000;      Error, B = 1000 not permitted
B = i;        Error, B = I not permitted
B + = (byte) i;    Ok
ch + + 1;       Error, ch = 1 not permitted
ch + = (char) 1;   Ok

The intuitive reason for each of the error is this a corresponding simple assignment would also have a error. End example]

[Note:compound assignment operations support lifted operators. Since a compound assignment x op= y is evaluated as either x = x op y or x = (T) (x op y), the rules of Evaluation implicit LY cover lifted operators. end note]

14.14.3 Event Assignment

If the left operand of a + + = = operator is an event, the expression is classified as a event access, and is evaluated as follows:

The instance expression, if any, of the event access is evaluated.

The right operand of the The + + or-= operator is evaluated, and, if required, converted to the ' left operand throu GH an implicit conversion (§13.1).

An event accessor of the "event is" invoked, with argument list consisting of the value computed on the previous step. If the operator was = =, the add accessor is invoked; If the operator was-=, the remove accessor is invoked.

An event assignment expression does is not yield a value. Thus, an event assignment expression are valid only into the context of a statement-expression (§15.6).

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.