Java Assignment Operators

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

int i = 25;

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

Assignment OperatorsExampleExplanation
=x = 9Value 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

Java Assignment Operators example

In this Java Program, We are using two integer variables a and Sum. Next, we are going to use these two variables to show the working functionality of all the Assignment Operators in Java Programming Language

// Program for Java Assignment Operators 
package JavaOperators;

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 1

Within this Assignment Operators example, the following statement will ask the user to enter integer value . Next, in this Java Program, we are going to assign the user input value to integer variable a.

System.out.println(" Please Enter any integer Value: ");
a = sc.nextInt();

The following 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.

System.out.println(" Please Enter any integer Value for Total: ");
Sum = sc.nextInt();

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

System.out.format(" Value of Sum = %d \n", Sum += a );

Sum += a means

Sum = Sum + a ==> 25 + 4 = 29

System.out.format(" Value of Sum = %d \n", Sum -= a );

Sum -= a means

Sum = Sum – a ==> 29 – 4 = 25

System.out.format(" Value of Sum = %d \n", Sum *= a );

Sum *= a means

Sum = Sum * a ==> 25 * 4 = 100

System.out.format(" Value of Sum = %d \n", Sum /= a );

Sum /= a means

Sum = Sum / a ==> 100 / 4 = 25

System.out.format(" Value of Sum = %d \n", Sum %= a );

Sum %= a means

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