KwickAcademy Course Topics · 6 min · free
Assignment operations
Learn Java assignment: = vs ==, compound operators +=, -=, *=, /=, %=, the hidden cast, chained assignment, increment and a swap program. = evaluates the right side, then stores it on the left; compound operators operate, store and include a hidden cast.
Follows the syllabus of: ISC Class 12 Computer Science (868)
On screen in this lesson
The assignment operator
| = stores the value on its right in the variable on its left |
| The right side is evaluated first |
| The left side must be a variable |
| = stores; == compares and gives true or false |
Compound assignment
| Operator | Means | If x = 17 |
|---|---|---|
| x += 3 | x = x + 3 | 20 |
| x -= 3 | x = x - 3 | 14 |
| x *= 3 | x = x * 3 | 51 |
| x /= 3 | x = x / 3 | 5 |
| x %= 3 | x = x % 3 | 2 |
Pause and predict
| int t = 17; t /= 5; |
| double d = 17; d /= 5; |
| What are t and d now? |
More assignment facts
| An assignment is an expression; its value is the value stored |
| String s = "Hi"; s += "!"; joins text: Hi! |
| Shift forms exist too: <<=, >>=, >>>= |
| = has the lowest precedence, so it runs last |
Quick recap
| = evaluates the right side, then stores it on the left |
| Compound: +=, -=, *=, /=, %= operate and store |
| Compound operators include a hidden cast |
| Chained a = b = c = 8 works right to left |
| x++ uses then adds; ++x adds then uses |
Quick answers
int m = 40; m += 5; m *= 2; m %= 7; What is m?
6.
int x = 5; int y = x++ + ++x; What are x and y?
7 and 12.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
The full lesson, in text
Hello students, welcome to Kwickprep. Why does byte b equals b plus one fail, while b plus equals one works? Today we will master assignment operations in Java, including a favourite exam trap.
The assignment operator is a single equals sign. It stores the value on its right into the variable on its left. Java first works out the right side completely, and only then stores the result. So the left side must be a variable, and writing five equals x is an error. Do not confuse it with double equals, which compares two values and gives true or false.
In maths, total equals total plus twenty is impossible. In Java, it is normal. Java reads the old value of total, which is fifty, and adds twenty. Then it stores seventy back into total. So the program prints seventy.
Java has short forms called compound assignment operators. Plus equals adds and stores, so seventeen becomes twenty. Minus equals subtracts and stores, giving fourteen. Star equals multiplies and stores, giving fifty one. Slash equals divides and stores, and with int it drops the decimal, giving five. Percent equals stores the remainder, which is two.
Let us trace this step by step. M starts at forty. M plus equals five makes it forty five. M star equals two makes it ninety. Then m percent equals seven stores the remainder of ninety divided by seven. Seven twelves are eighty four, so the remainder is six, and it prints six.
Now the trap from the start. In b plus five, Java turns the byte into an int before adding, so the result is an int. Storing that int back into a byte gives a compile error: possible lossy conversion. But b plus equals five contains a hidden cast back to byte. So this program compiles and prints fifteen.
You can assign one value to many variables in one line. This is called chained assignment. The assignment operator works from right to left, so eight goes into c first, then into b, then into a. Each variable holds eight, so a plus b plus c prints twenty four.
Plus plus and minus minus are also assignments, because they change the variable by one. Postfix x plus plus uses the value first, then increases it. Prefix plus plus x increases first, then uses it. Here x plus plus gives five and makes x six. Then plus plus x makes x seven and gives seven. So y is twelve, and it prints seven, space, twelve.
Pause and predict. First, int t is seventeen, and we do t slash equals five. Second, double d is seventeen, and we do d slash equals five. What are t and d? T is three, because int division drops the decimal part. D is three point four, because a double keeps the decimal part.
A classic exam program swaps two numbers without a third variable, using only assignments. A is four and b is nine. A becomes thirteen. Then b becomes thirteen minus nine, which is four. Then a becomes thirteen minus four, which is nine. It prints nine, space, four.
Here are a few more facts for higher marks. An assignment is itself an expression, and its value is the value that was stored. Plus equals also works on strings, so s plus equals exclamation joins the text to make Hi with an exclamation mark. Java also has shift assignment operators, like left shift equals. Assignment operators have the lowest precedence, so they run after everything else in the expression.
Let us revise what we learned today. The assignment operator evaluates the right side first, then stores it on the left. Compound operators like plus equals do the operation and store the result. They also include a hidden cast, so byte b plus equals five compiles. Chained assignment works from right to left. And x plus plus uses the value first, while plus plus x adds first. Trace every program on paper before you check the answer.
Courses that teach this
| Course | Unit |
|---|---|
| ISC Class 12 Computer Science (868) | Variables and Expressions |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.


