Assignment Operator in Java Explained with Examples (2025 Guide)

Assignment Operator in Java Explained with Examples (2025 Guide)

03 Jul 2025
Beginner
6.51K Views
21 min read
Learn with an interactive course and practical hands-on labs

Free Java Course With Certificate

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

OperatorMeaningExampleEquivalent to
=Simple assignmenta = 10;Assigns 10 to a
+=Add and assigna += 5;a = a + 5;
-=Subtract and assigna -= 3;a = a - 3;
*=Multiply and assigna *= 2;a = a * 2;
/=Divide and assigna /= 4;a = a / 4;
%=Modulus and assigna %= 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

This Java program declares two variables, assigns values to them, and then prints messages to the console confirming the assigned values. It serves as a simple illustration of variable declaration, assignment, and output in Java.

Output

value is assigned: 2024
name is assigned: ScholarHat

2. Addition Assignment Operator (+=) :

The (+=) operator adds the right operand to the left operand and assigns the result to the left operand.

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

The above code in the Java Online Editor initializes two variables, prints their initial values, performs an addition using the compound assignment operator, and prints the addition result. The output of the program will display the initial values of value1 and value2, followed by the modified value of value1 after the addition.

Output

value1 is assigned: 12
value2 is assigned: 24
value1 = 36

3. Subtraction Operator (-=):

The (-=) operator subtracts the right operand from the left operand and assigns the result to the left operand.

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

In this, the code initializes two variables, prints their initial values, performs a subtraction using the compound assignment operator, and prints the result of the subtraction. The Java Online Compiler will display the initial values of value1 and value2, followed by the modified value of value1 after the subtraction.

Output

value1 is assigned: 20
value2 is assigned: 10
value1 = 10

4. Multiplication Operator (*=):

The (*=) operator multiplies the left operand by the right operand and assigns the result to the left operand.

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

This Java code in the Java Compiler demonstrates the use of the compound assignment operator *= to multiply the value of value1 by value2 and assign the result back to value1. The console output reflects the assigned values and the result of the multiplication.

Output

value1 is assigned: 20
value2 is assigned: 10
value1 = 200

Read More - Java Developer Salary

5. Division Operator (/=):

The (/=) operator divides the left operand by the right operand and assigns the result to the left operand.

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

The compound assignment operator /= is used to divide the value of value1 by the value of value2 and assign the result back to value1. In this case, it operates value1 = value1 / value2. The result illustrates the impact of the /= operator on the variable value1.

Output

value1 is assigned: 20
value2 is assigned: 10
value1 = 2

6. Modulus Assignment Operator (%=):

The (%=) operator divides the left operand by the right operand and assigns the remainder to the left operand.

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

In this code, value1 is initially assigned the value 12, and value2 is assigned the value 24. The program then prints out these initial values. After that, the code uses the %= compound assignment operator to calculate the remainder of the division of value1 by value2. The result is then printed to the console, showing the updated value of value1 after the operation.

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

This example demonstrates the basic usage of simple and compound assignment operators in Java, providing a clear understanding of how these operators can be used to assign and manipulate variable values.

Output

= Operator : 10
+= Operator : 8
-= Operator : 8
*= Operator : 12
/= Operator : 4
%= Operator : 1

Read More

Assignment Operator Precedence and Associativity in Java

In Java, the assignment operator (=) has low precedence compared to most other operators, and it is right-to-left associative. This means when multiple assignment operators appear in a single statement, evaluation starts from the right.

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

In Java, when you assign one object to another using the = operator, you are not copying the actual object, but rather copying the reference (i.e., memory address) of that object. As a result, both variables point to the same object in memory—changes made through one reference are reflected in the other.

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

1. Using == Instead of =
Beginners often confuse the assignment operator (=) with the equality operator (==).
Incorrect: if (x = 5)
Correct: if (x == 5)
2. Assigning Incompatible Data Types
Assigning values of the wrong type leads to compile-time errors.
Incorrect: int x = 3.5;
Correct: double x = 3.5; or int x = (int) 3.5;
3. Misunderstanding Compound Operators
Using a += b doesn’t mean a = b; it means a = a + b.
Always understand the combined operation and assignment logic.
Summary
Assignment operators in Java are fundamental for manipulating variables by assigning values directly or performing arithmetic operations along with assignments in a concise manner. Understanding and mastering these operators is crucial for writing efficient and readable Java code. If you're interested in more tips and guidance, you may also consider our Free Java Certification Course, which can validate your skills and enhance your credibility in the field.

FAQs

Yes, you can use multiple assignment operators in a single statement.

Apart from the ones mentioned (+=, -=, *=, /=, %=), Java does not have additional built-in compound assignment operators for other arithmetic operations. However, you can achieve similar effects by combining other operators with the assignment operator.

They are classified into two main types: simple assignment operator and compound assignment operator.

1. Simple Assignment Operator
  • 1. (=) operator
2. Compound Assignment 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.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Live Training - Book Free Demo
.NET Solution Architect Certification Training
12 Jul
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
React Certification Training
12 Jul
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Microservices Certification Training
12 Jul
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Azure Developer Certification Training
14 Jul
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
17 Jul
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this