×

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

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute 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.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Java Sort String

In this article, you will be acknowledged about how to sort a string in Java. Introduction Firstly, let us revise what is string Strings are collections of characters that are frequently used in...

4 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.