Java Assignment Operators

The Java Assignment operators are useful to assign the values to the declared variables. The equals ( = ) operator is the most commonly used assignment operator. For example:

int i = 25;

The table below displays all the assignment operators in this programming language.

Java Assignment OperatorsExampleExplanation
=x = 25Value 25 is assigned to x
+=x += 9This is same as x = x + 9
-=x -= 9This is same as x = x – 9
*=x *= 9This is same as x = x * 9
/=x /= 9This is same as x = x / 9
%=x %= 9This is same as x = x % 9
&=x &= 9Same as x = x & y
^=x ^= 9x = x ^ 9
<<=x <<= 2x = x << 2
>>=x >>= 2x = x >> 2
>>>=x >>>= 2x = x >>> 2

Java Assignment Operators example

In this Java Program, we use two integer variables, a and Sum. Next, we will use these two variables to show the working functionality of all the Assignment Operators.

import java.util.Scanner;

public class AssignmentOperators {
	private static Scanner sc;
	public static void main(String[] args) {
		int a, Sum;
		sc = new Scanner(System.in);
		System.out.println(" Please Enter any integer Value: ");
		a = sc.nextInt();
		System.out.println(" Please Enter any integer Value for Total: ");
		Sum = sc.nextInt();
		
		System.out.format(" Value of Sum = %d \n", Sum += a );
		System.out.format(" Value of Sum = %d \n", Sum -= a );
		System.out.format(" Value of Sum = %d \n", Sum *= a );
		System.out.format(" Value of Sum = %d \n", Sum /= a );
		System.out.format(" Value of Sum = %d \n", Sum %= a );
	}
}
Java Assignment Operators Example

TIP: We can replace the Java assignment operators with the arithmetic operators (x += 1 is the same as x = x + 1). Similarly, we can replace Increment and Decrement Operators with assignment operators to increase or decrease the value by more than 1. I also request you to visit the Java Bitwise operators from the Java tutorial page to perform bit operations.

Within this example, the tenth line statement will ask the user to enter an integer value. Next, in this Java Program, we will assign the user input value to integer variable a.

The 12th line of the Java statement will ask the user to enter the integer value Sum. And then, we are going to assign the user input value to the integer variable Sum.

Next, we performed all the assignment operations on a and Sum variables using available assignment operators. Let us see the functionality of the Assignment Operators in the Java Programming Language.

Sum += a means
Sum = Sum + a (Arithmetic operation)

==> 25 + 4 = 29

Sum -= a means
Sum = Sum – a

==> 29 – 4 = 25

Sum *= a means
Sum = Sum * a

==> 25 * 4 = 100

Sum /= a means
Sum = Sum/ a

==> 100 / 4 = 25

Sum%= a means
Sum= Sum% a

==> 25 % 4 = 1 (Remainder of 25 / 4 is = 1)

What is the difference between x = x + 1 and x += 1 in Java?

Both the Java assignment operators (+=) and the arithmetic operator (+) allow addition. However, += performs addition and assigns the result to the left operand. On the other hand, + assigns the result to the third variable. From the above,

  • x = x + 1 uses the arithmetic operator (+) and increases the x value by 1.
  • x += 1 uses the assignment operator (+=) and increases the x value by 1.

Example

public class example {
public static void main(String[] args) {
int x = 10;
x = x + 1;
System.out.println(x);

int y = 10;
y += 1;
System.out.println(y);
}
}
11
11

If you observe the above program, both x = x + 1 and x += 1 act the same and return the same result. Why do we need two operators to perform the same operation? The += operator performs an implicit cast, which is not possible with the + operator.

When we add values to byte or short data types, the arithmetic operation throws a compilation error. For example, the program below throws a compilation error. Here, x + 1 is an integer operation, and it is not possible to save an integer in a byte. It requires an explicit cast.

public class example {
public static void main(String[] args) {
byte x = 10;
x = x + 1;
System.out.println(x);
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to byte

This time, we use the Java assignment operator (+=), and it performs the addition and returns the result without any issue.

public class example {
public static void main(String[] args) {
byte x = 10;
x += 1;
System.out.println(x);

short y = 20;
y += 30;
System.out.println(y);
}
}
11
50

Bitwise and Shift Assignment Operators in Java

In the following example, we show the bitwise and shift assignment operators’ functionality. Here, we declared the value of a to 12 because, at each step, the value of a changes (a &= b) changes the value of a.

public class example {
public static void main(String[] args) {
int a = 12;
int b = 8;
int c = 2;

System.out.println(a &= b);
a = 12;
System.out.println(a |= b);
a = 12;
System.out.println(a ^= b);
a = 12;
System.out.println(a <<= c);
a = 12;
System.out.println(a >>= c);
a = 12;
System.out.println(a >>>= c);
}
}
8
12
4
48
3
3