×

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment operator refers to those operators which are used to assign values written on the right side of the operator to the variable present on the left side of the operator. The data types of value and the variable should be identical to avoid compiler errors. The assignment operator has right-to-left associatively since the values present on the right end of the operand get assigned to the left end of the operator.

Syntax:

Variable operator(=)  operand

Types of Assignment Operators

1. Simple assignment operator (=)

It consists of only one symbol.

It simply assigns the value present on the right side of the operator to the left side variable.

Syntax: variable = value

Example  

a = 15;

b = 25;

Implementation 1:

Let us see a java program to understand the concept of simple assignment operators in a better way.

// Java code to understand the working of the "=" operator


import java.io.*;


class Simple_Assignment {
public static void main(String[] args)
{
// Declaration of variables
int x;
String s;


// Assigning values
x = 15;
s = "javaTpoint";


// Printing the values
System.out.println("The assigned number: " + x);
System.out.println("The assigned string: " + s);
}
}

Output:

Types of Assignment Operators in Java

Implementation2:

Let us see another java program to understand the concept of simple assignment operators in a better way.

// Java code to understand the working of the "=" operator


import java.io.*;


class Simple_Assignment {
public static void main(String[] args)
{
// Declaration of variables
int a1;
double a2;
char c;
String s;


// Assigning the values
a1 = 15;
a2 = 15.0;
c = 'A';
s = "javaTpoint";


// Printing the values
System.out.println("The assigned integer: " + a1);
System.out.println("The assigned double value: " + a2);
System.out.println("The assigned character: " + c);
System.out.println("The assigned string: " + s);
}
}

Output:

Types of Assignment Operators in Java

2. Compound assignment operator

It consists of two symbols. It contains one mathematical operator along with an equal to (=) symbol.

It assigns the value present on the right end of the operator to the variable written on the left after performing the indicated operation.

The following are its types.

  1. (+=) operator: This operator is a union of the ‘+’ and ‘=’ operators.

Working -It adds the current value of the variable on the left end to the value present on the right end and then assigns the resulting value to the variable on the left.

Syntax:

x += y

It is equivalent to x = x + y

Example

x += 40;
It is x = x + 40

Implementation:

Let us see a java program to understand the concept of compound assignment operators in a better way.

// Java code to understand the working of the "+=" operator


import java.io.*;


class Compound_Assignment {
public static void main(String[] args)
{


// Declaration of variables
int a1 = 15, a2 = 35;


System.out.println("first number = " + a1);
System.out.println("second number = " + a2);


// Adding &assigning the sum to a1
a1 += a2;


// Printing the assigned values
System.out.println("final value = " + a1);
}
}

Output:

Types of Assignment Operators in Java
  1. (-=) operator: This operator is a union of ‘-‘and ‘=’ operators.

Working -It subtracts the value written on the right end of the operator from the current value of the variable on the left end and then assigns the resulting value to the variable on the left.

Syntax:

x -= y

It is equivalent to x = x – y

Example

x -= 40;
It is x = x - 40

Implementation:

Let us see a java program to understand the concept in a better way.

// Java code to understand the working of the "-=" operator


import java.io.*;


class Compound_Assignment {
public static void main(String[] args)
{


// Declaration of variables
int a1 = 15, a2 = 35;


System.out.println("first number = " + a1);
System.out.println("second number = " + a2);


// Subtracting andassigning the difference to a1
a1 -= a2;


// Printing the assigned values
System.out.println("final value = " + a1);
}
}

Output:

Types of Assignment Operators in Java
  1. (*=) operator: This operator is a union of the ‘*’ and ‘=’ operators.

Working -It finds the product of the current value of the variable on the left and the value on the right and then assigns the resulting value to the operand on the left end. 

Syntax:

x *= y

It is equivalent to x = x * y

Example

x *= 40;
It is x = x * 40

Implementation:

Let us see a java program to understand the concept in a better way.

// Java code to understand the working of the "*=" operator


import java.io.*;


class Compound_Assignment {
public static void main(String[] args)
{


// Declaration of variables
int a1 = 15, a2 = 35;


System.out.println("first number = " + a1);
System.out.println("second number = " + a2);


// Multiplying andassigning the product to a1
a1 *= a2;


// Printing the assigned values
System.out.println("final value = " + a1);
}
}

Output:

Types of Assignment Operators in Java
  1. (/=) operator: This operator is a union of the ‘/’ and ‘=’ operators.

Working -It divides the current value of the variable present on the left endof the operator by the value on the right end and then assigns the quotient or the resulting value to the operand on the left end. 

Syntax:

x /= y

It is equivalent to x = x / y

Example

x /= 40;
It is x = x / 40

Implementation:

Let us see a java program to understand the concept in a better way.

// Java code to understand the working of the "/=" operator


import java.io.*;


class Compound_Assignment {
public static void main(String[] args)
{


// Declaration of variables
int a1 = 35, a2 = 15;


System.out.println("first number = " + a1);
System.out.println("second number = " + a2);


// Dividing and assigning values
a1 /= a2;


// Printing the assigned quotient to a1
System.out.println("final value = " + a1);
}
}

Output:

Types of Assignment Operators in Java
  • (%=) operator: This operator is a compound of the ‘%’ and ‘=’ operators.

Working -It divides the current value of the variable present on the left end of the operator by the value on the right and then assigns the remainder to the operand on the left end. 

Syntax:

x %= y

It is equivalent to x = x % y

Example

x %= 40;
It is x = x % 40

Implementation:

Let us see a java program to understand the concept in a better way.

// Java code to understand the working of the "%=" operator


import java.io.*;


class Compound_Assignment {
public static void main(String[] args)
{


// Declaration of variables
int a1 = 35, a2 = 15;


System.out.println("first number = " + a1);
System.out.println("second number = " + a2);


// Dividing and assigning the remainder to a1
a1 %= a2;


// Printing the assigned values
System.out.println("final value = " + a1);
}
}

Output:

Types of Assignment Operators in Java

Table of Operators

OperatorsExamplesMeaning  
=a1 = a2a1 = a2;  
+=a1 += a2a1 = a1 + a2;  
-=a1 -= a2a1 = a1 - a2;  
*=a1 *= a2a1 = a1 * a2;  
/=a1 /= a2a1 = a1 / a2;  
%=a1 %= a2a1 = a1 % a2;  

This table shows a list of assignment operators along with examples and meanings we studied previously.

Summary

We understood the meaning of assignment operator. We saw there exist two main types of java assignment operators – simple and compound. Further, we saw the six types of compound assignment operators offered in java language and dealt with each type with the aid of examples and java code.


Related Topics

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.