Compound Assignment Operators in Java

Compound assignment operators are the operators which are syntactically compact. It provides a syntax that is shorter than the normal ones and we use it for allocating the result to any variable for any bitwise or arithmetic calculation. Basically, before allocating the first operand’s result, the operation on the two operands is performed.

Types of compound assignment operators

These are all the possible types of compound assignment operators in language java:

+= 

This is the addition compound assignment operators assigning the value of addition.

For example, a=a+1 can be written as a+=1.

-=

This is the subtraction compound assignment operators assigning the value of subtraction.

For example, a=a-1 can be written as a-=1.

*=

This is the multiplication compound assignment operators assigning the value of multiplication.

For example, a=a*2 can be written as a*=2.

/= 

This is the division compound assignment operators assigning the value of division.

For example, a=a/2 can be written as a/=2.

%=

This is the modulo compound assignment operators assigning the value of the remainder of the division.

For example, a=a%2 can be written as a%=2.

&=

This is the Bitwise & assignment compound operators assigning the value of logical AND operation.

For example, a=a & b can be written as a&=2.

|=

This is the Bitwise | assignment compound operators assigning the value of logical OR operation.

For example, a=a | b can be written as a|=2.

^=

This is the Bitwise ^ compound assignment operators assigning the value of logical XOR operation.

For example, a=a ^ b can be written as a^=2.

>=

This is the right-shift compound assignment operators assigning the value of the signed right bit shift operation.

For example, a=a >> b can be written as a>>=2.

>>=

This is the right shift 0 filled compound assignment operators assigning the value of unsigned right bit shift operation.

For example, a=a >>> b can be written as a>>>=2.

<<=

This is the left shift compound assignment operators assigning the value of the signed left bit shift operation.

For example, a=a << b can be written as a<<=2.

There is an advantage of using the compound operators. This is that the complexity of the program decreases. They are faster in performing the operation of calculation and assigning. If there is a statement a=a*3, it will perform slower than a*=3.

Steps to remember- How these statements are written?

Step 1: We have to write the statement in the same format as it is given. The initial format is writing the operator between the operands.

Step 2: Now shift the arithmetic operator before the assignment operator.

Step 3: The operand equal to the “Left value” is removed. Left value is the value on the left-hand side of the assignment operator.

Step 4: By following the above steps, we would achieve the Arithmetic Compound Assignment Statement Expression.

Some rules for working out with assignment Compound operators

The expression containing the compound assignment operators are evaluated at runtime in any one of the two ways. These depend upon programming situations. The two ways of evaluation are:

  1. If the operand on the left side of the assignment or the “Left value” is not an array syntax, in this case,
    • firstly, evaluation is done on the left value operand so that it produces a variable. In the case where the abrupt completion of the left-hand operand takes place, assignment expressions also terminate in an abrupt way. Then, no evaluation occurs for the operand on the right-side and therefore, there is no assignment operation.
    • In an alternate case, firstly the left operand value is saved in a variable and then the evaluation of the operands on the right-side is done. If there is an abrupt termination of the evaluation, the assignment operation also terminates and therefore, there is no assignment operation.
    • The binary operation indicated by the compound assignment operator is performed on the stored value of the left-sided expression and the values of the right-sided operand. If there is an abrupt termination of the operation, then assignment expressions also terminate abruptly and therefore, none of the assignment takes place.
    • In other cases, the result obtained from the operation (binary) is changed to the data type of value of the left-sided variable. This conversion is put through the value sets which are defined by an appropriate standard. Lastly, the result obtained after conversion it is then transferred to the variable.
  2. If left values or the operands on the left-hand side are array expressions, then we consider the following cases,
    • Firstly, there is an evaluation of the references of an array that takes place on the sub-expressions to the left-sided value or the operand on the left-hand side. If there is an abrupt termination of the operation, then assignment expressions are also terminated abruptly. There is now no evaluation operation performed on the index of sub-expressions of left-sided value operand of array expressions & the operand on the right-side. This leads to a condition of no assignment.
    • If the first step is not followed, in this case, evaluation is performed on the sub-expression of the index to the left value operand of array expressions. In the case where the abrupt completion of the left-hand operand takes place, the assignment expressions also terminate in an abrupt way. Then, no evaluation occurs for the operand on the right-side and therefore, there is no assignment operation.
    • In another case, if the value of the sub-expression of array references or expression tends to null, then the assignment operation does not occur and therefore, then throws an exception known as NullPointerException.
    • In another case where all above points are not valid, here the value assigned to the sub-expression of array references definitely points to the array. We look at this value and if this value fulfills the conditions such as the value that is less than zero, or which is greater or equals to the array’s length etcetera, then in this case, no assignment operation occurs. Also, an exception is thrown by the program known as ArrayIndexOutOfBoundsException.
    • In the last case where all the above cases hold invalid, here one of the array’s component is selected which is referred by the sub-expression of array references. Selecting this component is based on the value of the index sub-expression. This value is saved in a variable. After this, the operands on the right-handed side are evaluated. If there is an abrupt termination of the operation, then expressions of assignments also terminate gets abruptly and therefore, none of the assignment does occur.

 

Example of resolving a statement containing assignment Compound operators

This is a fact that whenever a value that is bigger is assigned to a data type which is smaller in its construct and range, then to obtain a result with no errors, that is, error in compile time, we have to do an operation known as explicit typecasting. But, when we use compound arithmetic assignment operators in Java, the operation of internal type casting is performed automatically. We do not have to write an extra piece of code. Even when we assign some bigger data to a variable with small data types, we do not have to do type casting. Indeed, there are chances of loss in the information of data.

Let’s take an example for the correct code,

The code is same as:

Here the result is 7.6 but it automatically type-casted into short data type and the result obtained is 8.

This was a detailed description on the compound assignment operators in Java. Still, if you have any query, you can ask us. We would to answer all your questions.

Leave a Reply