Updated March 21, 2023
Introduction to Assignment Operators in Java
Java Assignment Operators are classified into two categories, such as simple and compound assignment operators. As the name says, Assignment operators are used for assigning the values to the variables involved in the operations. Simple assignment operators handle plain, uncomplicated operations like addition, subtraction, multiplication and division. Compound assignment operators are used when there are more logical operations are required in the code, like ^, &, %, <>, >>, <<, etc.
The Assignment Operator is generally of two types. They are:
- Simple Assignment Operator
- Compound Assignment Operator
The Simple Assignment Operator is used with the “=” sign where the left side consists of the operand, and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side.
The compound operator is used where +,-,*, and / is used along with the = operator.
Types of Assignment Operator
There are various task operators. Assignments would result in the value of the objective/target variable after the assignment.
Simple Assignment Operator
First, we are going to see and check the working of the Simple Assignment operator with the help of a Java program. The program includes assigning two values to number 1 and number 2 and then printing it in the output to show that the values have been assigned to the numbers.
Code:
class AssignmentOperator
{
public static void main(String[] args)
{
int n1, n2;
// Assigning 5 to number1
n1 = 5;
System.out.println(n1);
// Assigning value of variable number2 to number1
n2 = n1;
System.out.println(n2);
}
}
When we execute the print statement, we get the following as output. We are able to see that the two numbers which have been initialized earlier have been printed. Both the numbers were initialized with the value 5.
Output:
Compound Assignment Operator
Here, we are going to check the working of the compound assignment operator. The following is the list of compound assignment operators.
- += Compound additional assignment operator
- -= Compound subtraction assignment operator
- *= Compound multiplication assignment operator
- /= Compound division assignment operator.
The above mentioned are the four basic compound assignment operators that are present. There are other compound assignment operators which are present such as:
- %= Compound Modulo Assignment operator.
- &= Compound bitwise Assignment operator.
- ^= Compound bitwise ^ assignment operator.
- >>= Compound right shift assignment operator.
- >>>= Compound right shift filled 0 assignment operator.
- <<= Compound left shift assignment operator.
In this article, we are going to check the first four compound assignment operators in detail along with the other operators. The basic advantage of compound assignment operators is that it saves a lot of code within the Java language program.
1. Compound Additional Assignment Operator
This operator is used to add numbers to a variable continuously throughout the entire loop. We are going to see a program in which we have the sum of the first ith natural number with the help of a loop. In the for loop, we are going to use a Compound additional operator.
Code:
//Program to print the sum uptil ith natural number
import java.io.*;
public class Main
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number upto which you want to find the sum");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
int sum=0;//Initializing sum=0
//Beginning of for loop
for (int j=1; j<i; j++)
{
sum+= j;//Compound assignment operator being used here
}//end of for loop
System.out.println("Sum of first " +i+ " natural numbers = " +sum);
}// end of main
}// end of class
We see that when we enter the value as 10, that is, we get the sum of the first 10 natural numbers like 45.
Output:
2. Compound Subtraction Assignment Operator
This program can be used to delete a number from an existing bigger number. In the next program, we are going to see the deletion of numbers from a bigger number 100.
Code:
//Program to print the sum when certain numbers are subtracted
import java.io.*;
public class Subtract
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number upto which you want to subtract from the sum");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
int sum = 100;//Initializing sum=0
//Beginning of for loop
for (int j=1; j<=i; j++)
{
sum-= j;//Compound assignment operator being used here
}//end of for loop
System.out.println("Result " +sum);
}// end of main
}// end of class
In the sample code, we see that the number 5 is entered, and from the number 100, the sum until 5 is subtracted, we get the answer as 85.
Output:
3. Compound Multiplication Assignment Operator
This program can be used to multiply numbers up to a certain number that the user enters. We see a program that is used to print the multiplication of a certain number by numbers inside a for a loop.
Code:
//Program to print the multiplication uptil ith natural number
import java.io.*;
public class Multiply
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number upto which you want to print the multiplication");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
int prod=1;//Initializing prod=1
//Beginning of for loop
for (int j=1; j<=i; j++)
{
prod*= j;//Compound assignment operator being used here
}//end of for loop
System.out.println("Result " +prod);
}// end of main
}// end of class
We enter the number as 5, and then we see that the result is the multiplication of the number with numbers below. In other words, this program shows the factorial of a number in simple terms. We see the output of the program in the below screen.
Output:
4. Compound Division Assignment Operator
In this case, we are going to see the division of a number using the division operator. We won’t be using any kind of loop in this case, but we are going to see the numerator and the denominator. We will input the value of the numerator and divide it by 10 and produce the output to the user.
Code:
//Program to print the division of a number
import java.io.*;
public class Divide
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the numerator");//Print statement
int i=Integer.parseInt(br.readLine());//Taking input from user
i/=10;// Compound Division assignment operator
System.out.println("Result " +i);
}// end of main
}// end of class
In the program, we input 100 as a number, and we divide it by 10 using the Compound Division Assignment operator. We get the output finally as 10, as shown in the below screenshot.
Output:
5. Compound Operators (Remaining Operators)
In the below program, we will see the working of the remaining operators present. The remaining operators are %, ^, &, >>, << and >>>The following is the code and output.
Code:
class CompoundAssignment
{
public static void main(String args[])
{
byte b2 = 127;
b2 %= 7;
byte b3 = 120;
b3 &= 40;
short s1 = 300;
s1 ^= 100;
byte b4 = 127;
b4 >>= 3;
short s2 = 100;
s2 <<= 3;
short s3 = 200;
s3 >>>= 4;
System.out.println("Value of b2= "+b2);
System.out.println("Value of b3= "+b3);
System.out.println("Value of b4= "+b4);
System.out.println("Value of s1= "+s1);
System.out.println("Value of s2= "+s2);
System.out.println("Value of s3= "+s3);
}
}
In the output, we see the result of the compound assignment operations that were left. The output has been printed correspondingly.
Output:
Conclusion – Assignment Operators in Java
This article sees two kinds of Assignment operators- Simple Assignment operators and Compound Assignment operators. We see the working with the help of coding examples. There are advantages as well as disadvantages of using Compound Assignment operators. Assignment operators are used in all other programming languages like C, C++, Python, and wherever value has to be assigned to a variable. The only constraint is that the value has to be of the same data type as the variable which is declared.
Recommended Articles
This is a guide to Assignment Operators in Java. Here we discuss the Introduction and Types of Assignment Operators in Java, including Simple Assignment Operator, Compound Assignment Operator. You may also look at the following articles to learn more –