Augmented Assignment Operators Java

6.3.1 Augmented Assignment statements

Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement:

augmented_assignment_stmt: target augop expression_list augop: "+=" | "-=" | "*=" | "/=" | "%=" | "**=" | ">>=" | "<<=" | "&=" | "^=" | "|=" target: identifier | "(" target_list ")" | "[" target_list "]" | attributeref | subscription | slicing

(See section for the syntax definitions for the last three symbols.)

An augmented assignment evaluates the target (which, unlike normal assignment statements, cannot be an unpacking) and the expression list, performs the binary operation specific to the type of assignment on the two operands, and assigns the result to the original target. The target is only evaluated once.

An augmented assignment expression like can be rewritten as to achieve a similar, but not exactly equal effect. In the augmented version, is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.

With the exception of assigning to tuples and multiple targets in a single statement, the assignment done by augmented assignment statements is handled the same way as normal assignments. Similarly, with the exception of the possible in-place behaviour, the binary operation performed by augmented assignment is the same as the normal binary operations.

See About this document... for information on suggesting changes.

Because the design of Ada was taken much more closely from mathematics than some other languages... And so...

Assignment is not an operator

Operators have specific properties - they operate on quantities returning a result - while leaving the quantities themselves unchanged.

This is important - stick rigorously to this understanding of an "operator" and you make a lot of optimisations possible because the semantics are much more predictable. Essentially, operators don't have side effects. You can repeat them or factor out repeated ones, and you have a lot more freedom to reorder expressions without changing their results.

If you mistake assignment for an operator, ... well, basically you're screwed. Just ONE "operator" with side effects means you lose valuable properties for ALL operators ... for what? some notational convenience, a hugely fertile breeding ground for bugs, and no extra performance or efficiency.

Incidentally when I had to poke around inside GCC recently I found a function in its expression analyser that explicitly broke (intermediate representation for) and transformed it internally into (intermediate representation for) So the shorter form really doesn't appear to be any more efficient!

The same rationale applies (less strictly in Ada than VHDL) to functions - they are just operators in another guise, and pure functions (in VHDL that's every function without the word "impure" in its declaration!) don't have side effects.

Which is also why Ada has both functions and procedures : functions, operators and expressions are essentially similar (and ideally, stateless and side-effect free); procedures, assignments and statements are a separate category (procedure calls and assignments are forms of statement).

Separating the concepts and using the appropriate one for each task goes a long way to making clear programs that you can understand and probably do what you intended...

Oh and Ada-2012 has finally caught up with VHDL-2008 and Algol-W (1963) with if- and case-expressions...

It is obvious that the assignments here are still statements...

Just to make sure:

Assignment is not an operator

Ada's designers had an impressive and usually VERY clear grasp of what was possible without compromising integrity and what would just lead to a mess. While newer language features have been added, as compiler techniques developed enough to make them reliable, it has always been a cautious process and the Ada-83 subset is still there virtually intact.

0 thoughts on “Augmented Assignment Operators Java”


Leave a Comment

Your email address will not be published. Required fields are marked *