Assignment operator in Java

Assignment operator in Java

28 Mar 2024
Beginner
814 Views
16 min read
Learn via Video Course & by Doing Hands-on Labs

Java Programming For Beginners Free Course

Assignment Operators in Java: An Overview

We already discussed the Types of Operators in the previous tutorial Java. In this Java tutorial, we will delve into the different types of assignment operators in Java, and their syntax, and provide examples for better understanding. Because 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 operator's concepts, consider enrolling in the best Java Certification Course.

What are the Assignment Operators in Java?

Assignment operators in Java are used to assign values to variables. They are classified into two main types: simple assignment operator and compound assignment operator.

Syntax:

The general syntax for a simple assignment statement is:

variable = expression;

And for a compound assignment statement:

variable operator= expression;

Read More - Advanced Java Interview Questions

Types of Assignment Operators in Java

  1. Simple Assignment Operator: The Simple Assignment Operator is used with the "=" sign, where the operand is on the left side and the value is on the right. The right-side value must be of the same data type as that defined on the left side.
  2. Compound Assignment Operator: Compound assignment operators combine arithmetic operations with assignments. They provide a concise way to perform an operation and assign the result to the variable in one step. The Compound Operator is utilized when +,-,*, and / are used in conjunction with the = operator.

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

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 Java Online Course, which can validate your skills and enhance your credibility in the field.

FAQs

Q1. Can I use multiple assignment operators in a single statement?

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

Q2. Are there any other compound assignment operators in Java?

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.

Q3. How many types of assignment operators?

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


Share Article
Live Training Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this