Java Assignment Operators
Table of Contents + −
In the last lesson you learned about Java arithmetic operators. Updating a variable using its own value, like score = score + 10, is so common that Java gives you shorter ways to write it. These are the assignment operators.
🟰 The basic assignment
The = sign is the basic assignment operator.
- It puts the value on the right into the variable on the left.
- It does not mean “is equal to” like in math; it means “put this value into this name”.
- Java works out the whole right side first, then stores that single result on the left.
int score = 10;System.out.println(score);That two-step order matters when the variable is on both sides:
- In
score = score + 10, Java reads the oldscorefirst, adds 10, then writes the result back. - Reading happens before writing, so the two sides never fight each other.
- Every other assignment operator is just a shorter way to write this same idea.
int score = 10;score = score + 10; // right side first: 10 + 10 = 20, then store 20System.out.println(score);Output
20➕ Compound assignment operators
One pattern repeats everywhere: take a variable, do math on it, store the result back. A compound assignment operator shortens this by gluing the math operator onto the =.
score = score + 10becomesscore += 10, doing exactly the same thing.- The rule:
x OP= valuealways meansx = x OP value. - This saves typing the variable name twice.
Here are all five:
| Operator | Short form | Same as |
|---|---|---|
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x - 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| %= | x %= 5 | x = x % 5 |
Think of it like a bank balance you keep adjusting. You say “add 50”, “take away 30” instead of rewriting the whole balance. Watch the same total update step by step:
int total = 100;total += 50; // total = total + 50 -> now 150total -= 30; // total = total - 30 -> now 120total *= 2; // total = total * 2 -> now 240total /= 4; // total = total / 4 -> now 60System.out.println(total);Each line changes total using its own current value, so it walks 100 to 150 to 120 to 240 to 60.
Output
60The %= one surprises people. The % operator gives the remainder after division, so %= stores that remainder back:
int items = 17;items %= 5; // remainder of 17 / 5 is 2, so items becomes 2System.out.println(items);17 divided by 5 is 3 with 2 left over, so items becomes 2. Handy for wrapping a big count into a position from 0 to 4.
Output
2🔤 Using += to join text
The + operator does two jobs in Java:
- With numbers it adds.
- With text it joins, which is called concatenation.
- So
+=on aStringadds more text onto the end.
String name = "Al";name += "ex"; // name = name + "ex" -> "Alex"name += " Smith"; // now "Alex Smith"System.out.println(name);So name grows from "Al" to "Alex" to "Alex Smith". The space inside " Smith" is on purpose so the words don’t run together.
Output
Alex SmithYou can even join a number onto text with +=; Java turns the number into text first:
String label = "Score: ";int points = 90;label += points; // number 90 becomes the text "90"System.out.println(label);So label ends up as "Score: 90". The number 90 became the characters 9 and 0 and got glued on.
Output
Score: 90🔁 Chained assignment
You can set several variables to the same value in one line, called chained assignment:
- It works because
=hands back the value it just stored. - The next
=to its left can reuse that returned value.
int a, b, c;a = b = c = 0; // c gets 0, then b gets 0, then a gets 0System.out.println(a + " " + b + " " + c);Read it right to left: c = 0 runs and gives back 0, then b takes it, then a takes it too. A neat way to reset a few counters at once.
Output
0 0 0Use chaining sparingly
Chained assignment is handy for setting a few variables to the same starting value. But long chains get hard to read, and they only work when every variable is the right type. So keep chains short and obvious.
🎭 The hidden cast inside compound assignment
This trick is rarely taught and causes real confusion, so go slow:
- A
byteonly holds small whole numbers, from -128 to 127. - Java does the math as
int(a bigger type), sob + 5produces anint, not abyte. - You can’t drop an
intback into abytewithout saying you mean it; that step is a cast, written(byte).
So the plain version needs a cast:
byte b = 10;// b = b + 5; // ❌ error: b + 5 is an int, won't fit in a byte by itselfb = (byte) (b + 5); // ✅ the (byte) cast says "yes, store it as a byte"System.out.println(b);But the compound version works with no cast at all:
byte b = 10;b += 5; // ✅ works fine, no cast neededSystem.out.println(b);Both print 15. The reason b += 5 works but b = b + 5 does not:
- Java quietly adds the cast inside every compound operator, so
b += 5really meansb = (byte)(b + 5). b = b + 5is plain assignment, so you must add the cast.b += 5is compound, so Java adds it for you.
Output
15The hidden cast has a sharp edge: because it is silent, a compound operator can throw away part of a number without warning:
byte b = 100;b += 50; // 100 + 50 = 150, but a byte maxes out at 127System.out.println(b);150 is too big for a byte, so the hidden cast chops it down and you get -106. The compiler stays quiet because you used +=, so the convenience can hide a real bug.
Output
-106The takeaway
+= and friends always cast the result back to the variable’s own type. That saves you typing on small types. But it can also silently shrink a value that grew too big. So with byte and short, keep an eye on whether your numbers can overflow.
➕➕ Increment and decrement
Adding or subtracting exactly 1 is so common (especially in loops) that Java has dedicated operators:
- Increment
++adds 1. - Decrement
--subtracts 1.
int count = 5;count++; // same as count = count + 1, now 6count++; // now 7count--; // now 6System.out.println(count);So count++ is the shortest way to write count = count + 1. You’ll see ++ constantly in loops, stepping a counter up one at a time.
Output
6🔀 Pre vs post: a subtle point
You can write ++ before the variable (++count) or after it (count++):
- On its own line, both add 1 and behave the same.
- The difference only shows when you use the value in the same statement.
- Post
count++uses the current value first, then adds 1. - Pre
++countadds 1 first, then uses the new value.
This example prints the difference clearly:
int a = 5;System.out.println(a++); // prints 5, then a becomes 6System.out.println(a); // prints 6
int b = 5;System.out.println(++b); // b becomes 6 first, then prints 6With a++, Java prints the old value 5 then bumps a to 6. With ++b, Java bumps b to 6 first then prints 6. The position of the ++ decides when the add happens.
Output
566Keep it simple at first
When ++ sits alone on its own line, pre and post do the same thing, so you do not need to worry about the difference. The distinction only matters when you read the value in the same line. When in doubt, put ++ on its own line.
⚠️ Common Mistakes
A few assignment slip-ups to avoid:
- Confusing
=with==. A single=assigns; a double==checks if two things are equal. Mixing them up is a classic bug.
// ❌ Avoid: using = where you meant to compare// if (score = 10) ... // assigns 10, does not compare
// ✅ Good: == comparesif (score == 10) { System.out.println("Exactly ten");}- Assuming
+=needs a cast like=does. It doesn’t;+=already casts for you.
byte b = 10;// b += (byte) 5; // ❌ extra cast, not neededb += 5; // ✅ compound assignment casts for you-
Writing the operator backwards. It is
+=, not=+.x =+ 5is read asx = (+5), which just setsxto 5 and ignores its old value. -
Forgetting that small types can overflow. Because
+=hides the cast, abyteorshortcan wrap past its limit with no warning. -
Cramming
++into a bigger expression. Mixinga++into a larger line makes code hard to follow; keep++on its own line.
✅ Best Practices
Habits for clean assignment:
- Use compound operators.
total += 50is shorter and clearer thantotal = total + 50. - Trust the built-in cast on small types, but watch the range.
b += 5casts for you; just make sure the result still fits in abyteorshort. - Use
++and--for stepping by one. They make loop counters clean and obvious. - Keep increment simple. Put
++or--on its own line unless you have a clear reason not to. - Keep chains short.
a = b = c = 0is fine for resetting a few values, but don’t stretch it out.
🧩 What You’ve Learned
Nicely done. Let’s recap.
- ✅ The basic
=works out the right side first, then stores that result on the left. - ✅ Compound operators (
+=,-=,*=,/=,%=) shorten “update a variable with its own value”;x OP= vmeansx = x OP v. - ✅
+=also joins text onto aString, and can stick a number onto text. - ✅ Chained assignment like
a = b = c = 0sets several variables in one line, right to left. - ✅ Compound operators cast the result back for you, so
b += 5works whereb = b + 5needs a cast. But that hidden cast can silently overflow small types. - ✅
++adds 1 and--subtracts 1; post (a++) uses then adds, pre (++a) adds then uses. - ✅ Do not confuse
=(assign) with==(compare).
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is total += 50 the same as?
Why: += is a compound assignment: total += 50 means total = total + 50.
- 2
Given byte b = 10; why does b += 5 compile but b = b + 5 does not?
Why: Compound assignment includes a hidden cast, so b += 5 means b = (byte)(b + 5). Plain assignment has no hidden cast, so b + 5 (an int) needs an explicit (byte) cast.
- 3
If int a = 5; what does System.out.println(a++); print?
Why: Post-increment uses the current value first (5) and then adds 1, so it prints 5 and a becomes 6.
- 4
What does the chained assignment a = b = c = 0 do?
Why: Assignment returns the stored value, so c = 0 runs first and gives back 0, then b and then a take that same 0. All three become 0.
🚀 What’s Next?
You can store and update values smoothly now. But how does a program make decisions, like checking if a score is high enough? That starts with comparing values. Let’s look at comparison operators next.