Compound assignment operators in Java

Compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand.
The following are all possible assignment operator in java:

1.    += (compound addition assignment operator)
2.    -=  (compound subtraction assignment operator)
3.    *= (compound multiplication assignment operator)
4.    /= (compound division assignment operator)
5.    %= (compound modulo assignment operator)
6.    &= (compound Bitwise & assignment operator)
7.    |= (compound Bitwise | assignment operator)
8.    ^= (compound Bitwise ^ assignment operator)
9.    >>= (compound right-shift assignment operator)
10.    >>>=(compound right-shift filled 0 assignment operator)
11.    <<=(compound left-shift assignment operator)

Implementation of all compound assignment operator

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// Compound assignment operators
class Main {
public static void main(String args[])
    {
        byte b = 120;
        b += 10;
        byte b1 = 120;
        b1 *= 10;
        short s = 330;
        s -= 30;
        byte b2 = 127;
        b2 %= 7;
        byte b3 = 120;
        b3 &= 40;
        short s1 = 300;
        s1 ^= 100;
        byte b4 = 127;
        b4 >>= 3;
        short s2 = 200;
        s2 <<= 3;
        short s3 = 300;
        s3 >> >= 4;
        System.out.println(b);
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
}

chevron_right


Output:

-126
-80
1
40
15
300
328
1600
18

Rules for resolving the Compound assignment operators

At run time, the expression is evaluated in one of two ways.Depending upon the programming conditions:

  1. If the left-hand operand expression is not an array access expression, then:
    • First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.
    • Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
    • Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
    • Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion to the appropriate standard value set, and the result of the conversion is stored into the variable.
  2. If the left-hand operand expression is an array access expression, then:
    • First, the array reference sub-expression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index sub-expression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.
    • Otherwise, the index sub-expression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and the right-hand operand is not evaluated and no assignment occurs.
    • Otherwise, if the value of the array reference sub-expression is null, then no assignment occurs and a NullPointerException is thrown.
    • Otherwise, the value of the array reference sub-expression indeed refers to an array. If the value of the index sub-expression is less than zero, or greater than or equal to the length of the array, then no assignment occurs and an ArrayIndexOutOfBoundsException is thrown.
    • Otherwise, the value of the index sub-expression is used to select a component of the array referred to by the value of the array reference sub-expression. The value of this component is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

Examples : Resolving the statements with Compound assignment operators

We all know that whenever we are assigning a bigger value to a smaller data type variable then we have to perform explicit type casting to get the result without any compile-time error. If we did not perform explicit type-casting then we will get compile time error. But in the case of compound assignment operators internally type-casting will be performed automatically, even we are assigning a bigger value to a smaller data-type variable but there may be a chance of loss of data information. The programmer will not responsible to perform explicit type-casting. Let’s see the below example to find the difference between normal assignment operator and compound assignment operator.
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 is evaluated only once.

For example, the following code is correct:

short x = 4;
x += 6.6;

and results in x having the value 7 because it is equivalent to:

short x = 4;
x = (short)(x + 6.6);
x=10

Because here 6.6 which is double is automatically converted to short type without explicit type-casting.

Refer: When is the Type-conversion required?

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of normal addition operator
public class Test {
public static void main(String[] args)
    {
        // Normal assignment operator
        byte b = 10;
        b = b + 10;
        System.out.println(b);
    }
}

chevron_right


Output:

error: incompatible types: possible lossy conversion from int to byte

Explanation: In the above example, we are using normal assignment operator. Here we are assigning an int (b+1=20) value to byte variable (i.e. b) that’s results in compile time error. Here we have to do type-casting to get the result.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of compound addition assignment operator
public class Test {
public static void main(String[] args)
    {
        // compound assignment operator
        byte b = 10;
        b += 10;
        System.out.println(b);
    }
}

chevron_right


Output:

20

Explanation: In the above example, we are using compound assignment operator. Here we are assigning an int (b+1=20) value to byte variable (i.e. b) apart from that we get the result as 20 because In compound assignment operator type-casting is automatically done by compile. Here we don’t have to do type-casting to get the result.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of normal multiplication operator
public class Test {
public static void main(String[] args)
    {
        // Normal multiplication operator
        short s = 1270;
        s = s * 100;
        System.out.println(s);
    }
}

chevron_right


Output:

Error: incompatible types: possible lossy conversion from int to short
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of compound multiplication operator
public class Test {
public static void main(String[] args)
    {
        // Compound multiplication operator
        short s = 1270;
        s *= 100;
        System.out.println(s);
    }
}

chevron_right


Output:

-4072
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of normal addition operator on float
public class Test {
public static void main(String[] args)
    {
        // Normal addition operator
        float f = 1270.00f;
        f = f + 127.00;
        System.out.println(f);
    }
}

chevron_right


Output:

error: incompatible types: possible lossy conversion from double to float
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// behavior of compound addition operator on float
public class Test {
public static void main(String[] args)
    {
        // Compound addition operator
        float f = 1270.00f;
        f += 127.00;
        System.out.println(f);
    }
}

chevron_right


Output:

1397.0

Reference: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.