11
JulAssignment Operator in Java Explained with Examples (2025 Guide)
Assignment operators in Java are used to give values to variables. The basic one is =, which means "put this value into that variable."Java also has shortcuts called compound assignment operators like +=, -=, *=, and /=. These do some math and save the result in the same variable.
In this Java tutorial, we will delve into the different types of assignment operators in Java, their syntax, and provide examples for better understanding. Java is a flexible and widely used programming language. Assignment operators play a crucial role in manipulating and assigning values to variables. To further enhance your understanding and application of Java assignment operators' concepts, consider enrolling in the best Java Online Course Free With Certificate.
What are the Assignment Operators in Java?
Assignment operators in Java are used to assign values to variables. They are mainly of two types: the simple assignment operator (=), which directly assigns a value, and compound assignment operators like +=, -=, *=, etc., which combine arithmetic operations with assignment. For example, x += 5 is a shorthand for x = x + 5. These operators are commonly used in expressions, loops, and calculations in Java programs.
Syntax:
The general syntax for a simple assignment statement is:
variable = expression;
And for a compound assignment statement:
variable operator= expression;
Read More -
List of Assignment Operators in Java
Operator | Meaning | Example | Equivalent to |
= | Simple assignment | a = 10; | Assigns 10 to a |
+= | Add and assign | a += 5; | a = a + 5; |
-= | Subtract and assign | a -= 3; | a = a - 3; |
*= | Multiply and assign | a *= 2; | a = a * 2; |
/= | Divide and assign | a /= 4; | a = a / 4; |
%= | Modulus and assign | a %= 3; | a = a % 3; |
Types of Assignment Operators in Java
1. Simple Assignment Operator: The Simple Assignment Operator (=) in Java assigns the value on the right-hand side to the variable on the left. Both sides must be of compatible data types.Example: int a = 10
2. Compound Assignment Operator: The Compound Assignment Operators (like +=, -=, *=, /=) combine arithmetic operations with assignment, making the code more concise.Example: a += 5; is the same as a = a + 5;
Let's explore in-depth:
1. Simple Assignment Operator (=):
The equal sign (=) is the basic assignment operator in Java. It is used to assign the value on the right-hand side to the variable on the left-hand side.
Syntax
operator1 = operator2;
Example
import java.io.*;
class AssignmentOperator {
public static void main(String[] args)
{
int value;
String data;
value = 2024;
data = "ScholarHat";
System.out.println("value is assigned: " + value);
System.out.println("name is assigned: " + data);
}
}
Explanation
Output
value is assigned: 2024
name is assigned: ScholarHat
2. Addition Assignment Operator (+=) :
Syntax
operator1 += operator2;
This means,
operator1 = operator1 + operator2;
Example
import java.io.*;
class AssignmentOperator {
public static void main(String[] args)
{
int value1 = 12, value2 = 24;
System.out.println("value1 is assigned: " + value1);
System.out.println("value2 is assigned: " + value2);
value1 += value2;
System.out.println("value1 = " + value1);
}
}
Explanation
Output
value1 is assigned: 12
value2 is assigned: 24
value1 = 36
3. Subtraction Operator (-=):
Syntax
operator1 -= operator2;
This means,
operator1 = operator1 - operator2;
Example
import java.io.*;
class AssignmentOperator {
public static void main(String[] args)
{
int value1 = 20, value2 = 10;
System.out.println("value1 is assigned: " + value1);
System.out.println("value2 is assigned: " + value2);
value1 -= value2;
System.out.println("value1 = " + value1);
}
}
Explanation
Output
value1 is assigned: 20
value2 is assigned: 10
value1 = 10
4. Multiplication Operator (*=):
Syntax
operator1 *= operator2;
This means,
operator1 = operator1 * operator2;
Example
import java.io.*;
class AssignmentOperator {
public static void main(String[] args)
{
int value1 = 20, value2 = 10;
System.out.println("value1 is assigned: " + value1);
System.out.println("value2 is assigned: " + value2);
value1 *= value2;
System.out.println("value1 = " + value1);
}
}
Explanation
Output
value1 is assigned: 20
value2 is assigned: 10
value1 = 200
Read More - Java Developer Salary
5. Division Operator (/=):
Syntax
operator1 /= operator2;
This means,
operator1 = operator1 / operator2;
Example
import java.io.*;
class AssignmentOperator {
public static void main(String[] args)
{
int value1 = 20, value2 = 10;
System.out.println("value1 is assigned: " + value1);
System.out.println("value2 is assigned: " + value2);
value1 /= value2;
System.out.println("value1 = " + value1);
}
}
Explanation
Output
value1 is assigned: 20
value2 is assigned: 10
value1 = 2
6. Modulus Assignment Operator (%=):
Syntax
operator1 %= operator2;
This means,
operator1 = operator1 % operator2;
Example
import java.io.*;
class AssignmentOperator {
public static void main(String[] args)
{
int value1 = 20, value2 = 7;
System.out.println("value1 is assigned: " + value1);
System.out.println("value2 is assigned: " + value2);
value1 %= value2;
System.out.println("value1 = " + value1);
}
}
Explanation
Output
value1 is assigned: 20
value2 is assigned: 7
value1 = 6
Example of Assignment Operator in Java
Let's look at a few examples in our Java Playground to illustrate the usage of assignment operators in Java:
public class AssignmentOperatorsExample {
public static void main(String[] args) {
// Simple Assignment Operator
int a = 10;
// Compound Assignment Operator
int b = 5;
b += 3; // b is now 8
int c = 12;
c -= 4; // c is now 8
int d = 6;
d *= 2; // d is now 12
int e = 20;
e /= 5; // e is now 4
int f = 17;
f %= 4; // f is now 1
// Displaying the results
System.out.println("= Operator : " + a);
System.out.println("+= Operator : " + b);
System.out.println("-= Operator : " + c);
System.out.println("*= Operator : " + d);
System.out.println("/= Operator : " + e);
System.out.println("%= Operator : " + f);
}
}
Explanation
Output
= Operator : 10
+= Operator : 8
-= Operator : 8
*= Operator : 12
/= Operator : 4
%= Operator : 1
Read More
- Unary Operator in Java
- Arithmetic Operators in Java
- Relational Operators in Java
- Logical Operators in Java
- Ternary Operator in Java
Assignment Operator Precedence and Associativity in Java
int a, b;
a = b = 5;
System.out.println(a); // 5
System.out.println(b); // 5
Explanation
First, b = 5 is evaluated, assigning 5 to b. Then a = b assigns the same value to a. This chaining works due to right-to-left associativity.
How Assignment Works with Objects in Java
Example
class Student {
String name;
}
public class Example {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Alice";
Student s2 = s1; // s2 now points to the same object as s1
s2.name = "Bob"; // changing s2 also affects s1
System.out.println(s1.name); // Output: Bob
System.out.println(s2.name); // Output: Bob
}
}
Common Mistakes to Avoid with Assignment Operators in Java
FAQs
1. Simple Assignment Operator
- 1. (=) operator
- 1. (+=) operator
- 2. (-=) operator
- 3. (*=) operator
- 4. (/=) operator
- 5. (%=) operator
Take our Java skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.