An operator that is used to store a value into a particular variable is called an assignment operator in Java. In any programming language, the assignment operator is one of the most commonly used operators because it assigns values to variables.
In Java programming, assignment operations are generally categorized into three types:
- Simple Assignment
- Compound Assignment
- Assignment as Expression
Simple Assignment in Java
A simple assignment uses the = operator to assign a value directly to a variable. This is the most common assignment operator in Java. We can use simple assignment in two ways:
- To store or assign a value to a variable.
- To store the value of one variable into another variable.
General Syntax
v = expression;In the above syntax:
- v → This is a variable name that represents a memory location where a value will be stored.
- expression → This may be a constant, a variable, or a combination of constants, variables, and operators.
- = → This is an assignment operator used to assign the value of the expression to the variable.
Examples of Simple Assignment
Example 1: Assigning a constant value
int x = 10;In this example, we have declared a variable named x and assigned the value 10 to it using the assignment operator =.
Example 2: Assigning one variable to another
int x = 10;
int y = x;In this example:
- We have defined a variable named x and assigned the value 10 to it.
- Then, we have defined another variable named y and assigned the value stored in the variable x.
- If x = 10, then y will also store the value 10.
Compound Assignment in Java
A compound assignment operator in Java combines an arithmetic or bitwise operation with an assignment operation. It performs the calculation and assigns the result to the variable in a single step. The general form of a compound assignment operator in Java is:
variable operator= value;This is an equivalent to:
variable = variable operator value;Types of Compound Assignment Operators in Java:
- Addition assignment operator (+=)
- Subtraction assignment operator (-=)
- Multiplication assignment operator (*=)
- Division assignment operator (/=)
- Modulus assignment operator (%=)
Let’s understand each one with basic syntax and examples.
1. Addition Assignment Operator
The addition assignment operator (+=) in Java adds a value to the variable and assigns the result back to the variable. The general syntax is:
variable += value;
// This is equivalent to:
variable = variable + value;Example:
public class AdditionAssignmentExample {
public static void main(String[] args) {
int num = 10;
num += 5;
System.out.println("Value of num: " + num);
}
}Output:
Value of num: 15
In this example:
- We have defined a variable named num and assigned the value 10 to it.
- The expression num += 5 is equivalent to num = num + 5. Therefore, the new value of num becomes 15.
2. Subtraction Assignment Operator
The subtraction assignment operator (-=) in Java subtracts a value from the variable and assigns the result back to the variable. The general syntax is:
variable -= value;
// This is equivalent to:
variable = variable - value;Example:
public class SubtractionAssignmentExample {
public static void main(String[] args) {
int num = 20;
num -= 8;
System.out.println("Value of num: " + num);
}
}Output:
Value of num: 12
In this example:
- We have defined a variable named num and assigned the value 20 to it.
- The expression num -= 8 is equivalent to num = num – 8. Therefore, the new value of num becomes 12.
3. Multiplication Assignment Operator
The multiplication assignment operator (*=) in Java multiplies the variable by a value and assigns the result back to the variable. The general syntax is:
variable *= value;
// This is equivalent to:
variable = variable * value;Example:
public class MultiplicationAssignmentExample {
public static void main(String[] args) {
int num = 6;
num *= 4;
System.out.println("Result: " + num);
}
}Output:
Result: 24
In this example:
- We have defined a variable named num and assigned the value 6 to it.
- The expression num *= 4 is equivalent to num = num * 4. Therefore, the new value of num becomes 24.
4. Division Assignment Operator
The division assignment operator (/=) in Java divides the variable by a value and assigns the result back to the variable. The basic syntax is:
variable /= value;
// This is equivalent to:
variable = variable / value;Example:
public class DivisionAssignmentExample {
public static void main(String[] args) {
int num = 40;
num /= 5;
System.out.println("Result: " + num);
}
}Output:
Result: 8
In this example:
- We have defined a variable named num and assigned the value 40 to it.
- The expression num /= 5 is equivalent to num = num / 5. Therefore, the new value of num becomes 8.
5. Modulus Assignment Operator
The modulus assignment operator (%=) in Java calculates the remainder and assigns it to the variable. The basic syntax is:
variable %= value;
// This is equivalent to:
variable = variable % value;Example:
public class ModulusAssignmentExample {
public static void main(String[] args) {
int num = 17;
num %= 5;
System.out.println("Remainder: " + num);
}
}Output:
Remainder: 2
In this example:
- We have defined a variable named num and assigned the value 17 to it.
- The expression num %= 5 is equivalent to num = num % 5. So, 17 % 5 gives the remainder 2, and the value 2 is stored in num.
Advanced Example: Using Multiple Assignment Operators
public class CompoundAssignmentExample {
public static void main(String[] args)
{
int x = 20, y = 30, z = 50;
x += y;
y -= x + z;
z *= x * y;
System.out.println("x = " +x );
System.out.println("y = " +y );
System.out.println("z = " +z );
}
}Output:
x = 50 y = -70 z = -175000
In this example:
- The expression x += y; will be evaluated in the following steps:
- x += y; is equivalent to x = x + y;
- x = 20 + 30;
- x = 50;
- The expression y -= x + z; is equivalent to y = y – (x + z);
- y = y – ( 50 + 50);
- y = 30 – 100;
- y = -70;
- The expression z *= x * y; is equivalent to z = z * (x * y);
- z = z * (50 * (-70));
- z = 50 * (-3500);
- z = -175000;
Assignment as Expression in Java
In Java, an assignment operation can also be considered as an expression because the operation produces a result. The result of the expression is a value that is stored in a variable.
We mainly use assignment as an expression when performing more than one assignment in a single statement. For example:
int x = y - z + 4;Here, the expression y – z + 4 is evaluated first, and then its result is stored into the variable x. Now, let’s take an example program where we will use an assignment as an expression.
Example 1: Chained Assignment
public class ChainedAssignmentExample {
public static void main(String[] args) {
int a, b, c;
a = b = c = 5;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}Output:
50 50 50
This is an example of chained assignment in Java. Chained assignment allows you to assign the same value to multiple variables in a single statement. The assignment operator (=) is evaluated from right to left. When you are creating a chain of any number of variables as long as the data types are compatible.
Example 2:
public class Expression {
public static void main(String[] args)
{
int a = 19, b = 31, c = 50;
a += 1;
b -= 1;
c *= 2;
int x = (10 + a);
int y = x + 100;
int z = x + y + c;
System.out.println("Value of a: " +a);
System.out.println("Value of b: " +b);
System.out.println("Value of c: " +c);
System.out.println("Value of x: " +x);
System.out.println("Value of y: " +y);
System.out.println("Value of z: " +z);
}
}Output:
Value of a: 20 Value of b: 30 Value of c: 100 Value of x: 30 Value of y: 130 Value of z: 260
In this example, the following steps have been performed to evaluate the above expressions of the program.
- a += 1;
a = a + 1;
a = 19 + 1;
a = 20; - b -= 1;
b = b – 1;
b = 31 – 1;
b = 30; - c *= 2;
c = c * 2;
c = 50 * 2;
c = 100; - x = (10 + a);
x = 10 + 20; Here, the value of x will be 20, not 19.
x = 30; - y = x + 100;
y = 30 + 100; Here, the value of x will be 30.
y = 130; - z = x + y + c;
z = 30 + 130 + 100; Here, the value of c will be 100.
z = 260;
Real-Time Example: Updating Account Balance
Let us take a simple real-time example where we will use Java assignment operators in updating a bank account balance.
public class BankExample {
public static void main(String[] args) {
int balance = 1000;
balance += 500; // deposit
balance -= 200; // withdraw
System.out.println("Final Balance: " + balance);
}
}Output:
Final Balance: 1300
Common Mistakes When Using Assignment Operators in Java
1. You cannot use an expression on the left-hand side of the = operator. The left-hand side must always be a variable. For example, x + y = 20; is an invalid because x + y is an expression, not a variable. Therefore, Java cannot store the value 20 in it.
2. You cannot use a literal or constant value on the left-hand side of = operator. For example, 20 = a; is also invalid because 20 is a constant value, not a variable. A constant cannot store another value.







