All About Assignment Operator in Java

All About Assignment Operator in Java

6 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Apr 9, 2024 13:18 IST

Have you ever wondered how assignment operators function in Java? They are special symbols used for assigning values to variables, with the most basic one being the equal sign (=). Additionally, Java offers compound assignment operators like +=, -=, and *= that combine arithmetic operations with assignment for more concise code. Let's understand more!

Operators in Java are special symbols or keywords that are used to perform operations on variables and values. These operations can range from basic mathematical calculations to complex logical comparisons. Operators in Java are categorized into several types based on their functionality. In this blog, we will learn about one of its types in detail, which is the assignment operator!

Table of Content

 

Check out Java courses here!

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

โ€“ / โ€“
350 hours
Free
6 months
โ€“ / โ€“
4 months
โ€“ / โ€“
โ€“ / โ€“
โ€“ / โ€“
1 month
โ‚น40 K
7 weeks
โ‚น7.8 K
3 months
โ‚น8.47 K
2 months
โ‚น7 K
6 months
โ‚น4 K
2 months

What is an Assignment Operator in Java?

In Java, an assignment operator is a fundamental operator used to assign a value to a variable. The most common assignment operator is the equals sign (=). When you use the assignment operator, the right-hand side of the operator is evaluated, and the result is stored in the variable on the left-hand side.

For example,


 
int a = 5; // assigns the value 5 to the variable 'a'
Copy code

In this example, 5 is assigned to the variable a.

Java also supports compound assignment operators, which combine an arithmetic, bitwise, or shift operation with an assignment. These operators provide a shorthand way to update the value of a variable. Examples include +=, -=, *=, /=, and %=.

Syntax of Assignment Operator

The basic syntax of the assignment operator (=) in Java is given below.

variable = expression;

Here,

  • variable: This is the variable where the value is to be stored. The variable must have been declared before its use.
  • expression: This can be a value, another variable, or any expression that results in a value. The expression should be compatible with the type of the variable.
  • =: The assignment operator itself.

Types of Assignment Operator with Example

1. Simple Assignment Operator ( = )

Usage: Assigns the value on its right to the variable on its left.

Example


 
public class SimpleAssignment {
public static void main(String[] args) {
int a = 5; // Assigns 5 to variable 'a'
System.out.println("After simple assignment, a = " + a);
}
}
Copy code

Output

After simple assignment, a = 5

2. Addition Assignment Operator ( += )

Usage: Adds the value on the right to the variable on the left and then assigns the result to the variable on the left.

Example


 
public class AdditionAssignment {
public static void main(String[] args) {
int a = 5;
a += 3; // Equivalent to a = a + 3; Now 'a' is 8
System.out.println("After addition assignment, a = " + a);
}
}
Copy code

Output

After addition assignment, a = 8

3. Subtraction Assignment Operator ( -= )

Usage: Subtracts the value on the right from the variable on the left and then assigns the result to the variable on the left.

Example


 
public class SubtractionAssignment {
public static void main(String[] args) {
int a = 8;
a -= 2; // Equivalent to a = a - 2; Now 'a' is 6
System.out.println("After subtraction assignment, a = " + a);
}
}
Copy code

Output

After subtraction assignment, a = 6

4. Multiplication Assignment Operator ( *= )

Usage: Multiplies the variable on the left by the value on the right and then assigns the result to the variable on the left.

Example


 
public class MultiplicationAssignment {
public static void main(String[] args) {
int a = 6;
a *= 2; // Equivalent to a = a * 2; Now 'a' is 12
System.out.println("After multiplication assignment, a = " + a);
}
}
Copy code

Output

After multiplication assignment, a = 12

5. Division Assignment Operator ( /= )

Usage: Divides the variable on the left by the value on the right and then assigns the result to the variable on the left.

Example


 
public class DivisionAssignment {
public static void main(String[] args) {
int a = 12;
a /= 2; // Equivalent to a = a / 2; Now 'a' is 6
System.out.println("After division assignment, a = " + a);
}
}
Copy code

Output

After division assignment, a = 6

6. Modulus Assignment Operator ( %= )

Usage: Applies the modulus operation to the variable on the left by the value on the right and then assigns the result to the variable on the left.

Example


 
public class ModulusAssignment {
public static void main(String[] args) {
int a = 6;
a %= 4; // Equivalent to a = a % 4; Now 'a' is 2
System.out.println("After modulus assignment, a = " + a);
}
}
Copy code

Output

After modulus assignment, a = 2

Java String Compare: A Guide to Effective String Comparison in Java
Java String Compare: A Guide to Effective String Comparison in Java
Have you ever wondered how to compare strings in Java? You can use various methods for that, like using equals(), using == Operator, using equalsIgnoreCase(), using compareTo() and using compareToIgnoreCase()....read more

What are Identifiers in Java?
What are Identifiers in Java?
Have you ever wondered how Java keeps everything organized and accessible? It's all because of identifiers. Those unique labels assigned to variables, methods, and classes. These crucial elements of Java...read more

Getting Started with Java Hello World Program
Getting Started with Java Hello World Program
Do you know the significance of the "Hello World" program in Java? It's the first step for many into the world of programming, serving as a simple yet profound introduction...read more

Examples Showing Usage of Assignment Operator

1. Accumulating a Sum

Problem Statement: Calculate the sum of numbers from 1 to 5 using a loop and assignment operators.


 
public class SumCalculation {
public static void main(String[] args) {
int sum = 0; // Initialize sum
for (int i = 1; i <= 5; i++) {
sum += i; // Accumulate sum using addition assignment operator
}
System.out.println("Sum of numbers from 1 to 5: " + sum); // Output the sum
}
}
Copy code

Output

Sum of numbers from 1 to 5: 15

2. Chained Assignment Operators

Problem Statement: Initialize several variables to the same value using chained assignment operators.


 
public class ChainedAssignmentExample {
public static void main(String[] args) {
int a, b, c; // Declaration of multiple variables
a = b = c = 10; // Chained assignment. All variables 'a', 'b', and 'c' are assigned the value 10
System.out.println("Value of a: " + a); // Display the value of 'a'
System.out.println("Value of b: " + b); // Display the value of 'b'
System.out.println("Value of c: " + c); // Display the value of 'c'
}
}
Copy code

Output

Value of a: 10
Value of b: 10
Value of c: 10

3. Using the Division Assignment Operator

Problem Statement: Divide a variable's value by a number and update it.


 
public class DivisionAssignmentExample {
public static void main(String[] args) {
int number = 20; // Initialize the variable
System.out.println("Original value: " + number); // Display the original value
number /= 4; // Divide 'number' by 4 using the division assignment operator
// Equivalent to number = number / 4
System.out.println("Value after division: " + number); // Display the updated value
}
}
// Output:
// Original value: 20
// Value after division: 5
Copy code

Output

Original value: 20
Value after division: 5

Array Programs in Java | Beginner to Expert Level
Array Programs in Java | Beginner to Expert Level
Array programs in Java traverse from basic single-dimensional arrays to complex multi-dimensional arrays and dynamic arrays using ArrayList. From initializing and accessing array elements, to advanced operations like sorting and...read more

A Guide to Power Function in Java
A Guide to Power Function in Java
Have you ever wondered how mathematical power functions are implemented in programming languages like Java? In Java, the Math.pow() function is a powerful tool used to raise a number to...read more

Java Comments | About, Types and Examples
Java Comments | About, Types and Examples
Do you know what makes the code more readable? Comments, as they provide valuable context and explanations about the code, making it easier for both the original developers and others...read more

Conclusion

Thus, assignment operators in Java are fundamental tools that allow developers to store values in variables and modify those values efficiently. They are crucial for almost every operation in Java, from basic variable initialization to complex expression evaluation. 

FAQs

What is the Assignment Operator in Java?

The assignment operator in Java, denoted by =, is used to assign a value to a variable. For example, int a = 5; assigns the value 5 to the variable a.

Can the Assignment Operator be Used to Assign Values to Multiple Variables Simultaneously?

Yes, in Java, you can use the assignment operator to assign the same value to multiple variables in one line. For example, int a = b = c = 100; assigns the value 100 to a, b, and c.

How Does the Assignment Operator Work with Objects?

When used with objects, the assignment operator copies the reference of the object, not the object itself. For example, Object obj1 = new Object(); Object obj2 = obj1; Here, obj2 will refer to the same object as obj1.

What is the Difference Between the Assignment Operator and the Equality Operator?

The assignment operator (=) is used to assign values to variables, while the equality operator (==) is used to compare two values or object references to check if they are equal.

Can the Assignment Operator be Used with Different Data Types?

The assignment operator can be used with different data types, but the type on the right-hand side of the assignment must be compatible with the type of the variable on the left-hand side. In cases where they are not directly compatible, type casting may be necessary. For example, double d = 10; is valid because the integer 10 is implicitly cast to a double.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio