Open In App

Compound Assignment Operators in Java

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on two operands before assigning the result to the first operand.

The following are all possible assignment operators 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 Operators

Example:

Java
// Java program to demonstrates the working
// of Compound assignment operators

class Geeks {
    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);
    }
}

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:

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.

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.

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. If we do not perform explicit type-casting, we will get a compile-time error. But in the case of compound assignment operators, type-casting is performed automatically.

Even if we are assigning a bigger value to a smaller data-type variable there will be a chance of lossing data, in that case the programmer will not responsible to perform explicit type-casting.

For example, a compound assignment expression of the form E1 op= E2 is equivalent to:

E1 = (T) ((E1) op (E2)),


Note: Here T is the type of E1, except that E1 is evaluated only once.

short x = 4;

x += 6.6;


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

short x = 4;

x = (short)(x + 6.6);

Note: Here, 6.6 which is double is automatically converted to short without explicit type-casting. Refer: When is the Type-conversion required?

Normal Assignment vs Compound Assignment Operator

Now, we are going to understand the difference between normal assignment and compound assignment with the help of practical examples for better understanding.

Normal Assignment

Example:

Java
// Java program to demonstrates the
// behavior of normal addition operator

public class Geeks {
    public static void main(String[] args) {
        // Normal assignment operator
        byte b = 10;
        b = b + 10; 
        System.out.println(b);
    }
}

Output:

Output


Explanation: In the above example, we are using the normal assignment operator. In this, we are assigning an int b + 10 = 20 value to a byte variable b, which results in a compile-time error. In this case, we need to explicitly cast the result to byte.


Compound Assignment

Example:

Java
// Java program to demonstartes the working
// of compound addition assignment operator

public class Geeks {
    public static void main(String[] args) {
        // compound assignment operator
        byte b = 10;
        b += 10;
        System.out.println(b);
    }
}

Output
20

Explanation: In the above example, we are using the compound assignment operator. In this the result of the operation b+10 = 20 is automatically cast to byte.


Normal Multiplication Operator

Example:

Java
// Java program to demonstrates the working of
// of normal multiplication operator

public class Geeks {
    public static void main(String[] args) {
        // Normal multiplication operator
        short s = 1270;
        s = s * 100; 
        System.out.println(s);
    }
}

Output:

Output


Explanation: In the above example, we are trying to multiply a short by 100. In this the result is an int and Java cannot implicitly cast it back to short and cause a compile-time error.

Compound Multiplication Operator

Example:

Java
// Java program to demonstrates the
// working of compound multiplication operator

public class Geeks {
    public static void main(String[] args) {
        // Compound multiplication operator
        short s = 1270;
        s *= 100;
        System.out.println(s);
    }
}

Output
-4072


Normal Addition Operator on float

Example:

Java
// Java program to demonstrates the working
// of normal addition operator on float

public class Geeks {
    public static void main(String[] args) {
        // Normal addition operator
        float f = 1270.00f;
        f = f + 127.00; 
        System.out.println(f);
    }
}

Output:

Output


Explanation: In the above example, the double result of f + 127.00 cannot be assigned to a float variable without explicit type casting.

Compound Addition Operator on float

Example:

Java
// Java program to demonstrates the working 
// of compound addition operator on float

public class Geeks {
    public static void main(String[] args) {
        // Compound addition operator
        float f = 1270.00f;
        f += 127.00;
        System.out.println(f);
    }
}

Output
1397.0

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


Next Article
Article Tags :
Practice Tags :

Similar Reads