Java Tutorial

Assignment Operators in Java (=, +=, -=, *=, /=, %= and More) with Examples

TGTeam GyaniPanditNovember 21, 20264 min read

Assignment operators in Java store a value in a variable. The basic one is =. Java also has compound assignment operators such as += and *=, which do a calculation and store the result in the same variable in one step. This page covers all of them with examples, outputs and the mistakes beginners usually make.

List of Assignment Operators in Java

Operator Name Example Same as
= Simple assignment x = 5 –
+= Add and assign x += 5 x = x + 5
-= Subtract and assign x -= 5 x = x - 5
*= Multiply and assign x *= 5 x = x * 5
/= Divide and assign x /= 5 x = x / 5
%= Remainder (modulus) and assign x %= 5 x = x % 5
&= Bitwise AND and assign x &= 5 x = x & 5
|= Bitwise OR and assign x |= 5 x = x | 5
^= Bitwise XOR and assign x ^= 5 x = x ^ 5
<<= Left shift and assign x <<= 2 x = x << 2
>>= Right shift and assign x >>= 2 x = x >> 2
>>>= Unsigned right shift and assign x >>>= 2 x = x >>> 2

“Same as” is true for the result, with one important difference about types, explained further below.

Example: Arithmetic Assignment Operators

public class AssignmentDemo {
    public static void main(String[] args) {
        int num = 10;      // simple assignment

        num += 5;          // 10 + 5
        System.out.println(num);

        num -= 3;          // 15 - 3
        System.out.println(num);

        num *= 2;          // 12 * 2
        System.out.println(num);

        num /= 5;          // 24 / 5 (integer division)
        System.out.println(num);

        num %= 3;          // remainder of 4 / 3
        System.out.println(num);
    }
}

Output:

15
12
24
4
1

Notice the fourth line: 24 / 5 gives 4, not 4.8, because both values are int. With a double variable, /= keeps the decimal part:

double d = 10;
d /= 4;
System.out.println(d);   // 2.5

int i = 10;
i /= 4;
System.out.println(i);   // 2

Example: Bitwise and Shift Assignment Operators

int flags = 12;      // binary 1100

flags &= 10;         // 1100 & 1010 = 1000
System.out.println(flags);

flags |= 3;          // 1000 | 0011 = 1011
System.out.println(flags);

flags ^= 5;          // 1011 ^ 0101 = 1110
System.out.println(flags);

flags <<= 2;         // shift left by 2 = multiply by 4
System.out.println(flags);

flags >>= 3;         // shift right by 3 = divide by 8
System.out.println(flags);

Output:

8
11
14
56
7

The difference between >>= and >>>= shows up with negative numbers. >> keeps the sign; >>> fills the left side with zeros:

int a = -16;
a >>= 2;
System.out.println(a);   // -4

int b = -16;
b >>>= 2;
System.out.println(b);   // 1073741820

Compound Assignment Does an Automatic Cast

This is the one place where x += y is not exactly the same as x = x + y. A compound assignment automatically converts the result back to the variable’s type.

byte b = 10;
b += 5;          // compiles: result is cast back to byte
// b = b + 5;    // compile error: possible lossy conversion from int to byte
System.out.println(b);   // 15

int x = 10;
x += 2.7;        // 12.7 is cast to int
System.out.println(x);   // 12

char c = 'A';
c += 2;
System.out.println(c);   // C

The hidden cast is convenient, but it can also hide overflow:

short s = 30000;
s += 30000;              // 60000 does not fit in a short
System.out.println(s);   // -5536

+= with Strings

+= also joins (concatenates) strings:

String site = "Gyani";
site += "Pandit";
System.out.println(site);   // GyaniPandit

Inside a long loop, prefer StringBuilder, because every += on a String creates a new object.

Chained Assignment

The = operator works from right to left, so you can give the same value to several variables in one line:

int p, q, r;
p = q = r = 7;
System.out.println(p + " " + q + " " + r);   // 7 7 7

Common Mistakes

  • Writing =+ instead of +=. x =+ 5; compiles, but it means x = +5, so x becomes 5.
  • Confusing = with ==. = assigns a value; == compares two values.
  • Expecting decimals from /= on an int. Use a double variable if you need the fraction.
  • Using a variable before giving it a value. Local variables must be assigned before += can use them.

Frequently Asked Questions

How many assignment operators are there in Java?

Twelve: the simple = and eleven compound operators (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=).

Is x += 1 the same as x++?

Both increase x by 1. The difference is in the value of the expression: x++ returns the old value, while x += 1 returns the new value.

Why does b += 5 work for a byte but b = b + 5 does not?

In b + 5, Java promotes b to int, so the result is an int. A compound assignment casts the result back to byte automatically; a plain assignment does not.

Related: Arithmetic operators in Java · Java Math class

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses