Assignment Operators in Java

Assignment Operators are used to assign a value to a variable. Assignment Operators are classified into two types.

  • Simple Assignment Operators
  • Shorthand Assignment Operators


Simple Assignment Operators

The java basic assignment operator uses a single equal sign to assign a value to the variable. ( = ).

Syntax

variable = expression ;

The assignment operator works from right to left. The expression on the right hand side is evaluated first. The output of the expression is assigned to the variable specified in the left side of the assignment operator

Example

String name;
Name =”Animal”;


Shorthand Assignment Operators

Shorthand assignment operator is a short form of assignment operator. Java supports various shorthand assignment operators.

OperatorExampleEquivalent to
+=a += ba = a + b
-=a -= ba = a – b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
&=a &= ba = a & b
|=a |= ba = a | b
^=a ^= ba = a ^ b
<<=a <<= ba = a << b
>>=a >>= ba = a >> b
>>>=a >>>= ba = a >>> b


Arithmetic Addition Assignment Operator ( “+=” )

The following example shows the addition of two numbers using arithmetic plus operator and the shorthand “+ =” assignment operator are the same.

package com.yawintutor;

public class AssignmentPlusEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a += b ;
		System.out.println("\n(a += b)    : " + a);

		a = 6;
		a = a + b;
		System.out.println("\n(a = a + b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a += b)    : 8

(a = a + b) : 8


Arithmetic Subtraction Assignment Operator ( “-=” )

The following example shows the subtraction of two numbers using arithmetic minus operator and the shorthand “-=” assignment operator are the same.

package com.yawintutor;

public class AssignmentMinusEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a -= b ;
		System.out.println("\n(a -= b)    : " + a);

		a = 6;
		a = a - b;
		System.out.println("\n(a = a - b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a -= b)    : 4

(a = a - b) : 4


Arithmetic Multiplication Assignment Operator ( “*=” )

The following example shows the multiplication of two numbers using arithmetic multiplication operator and the shorthand “*=” assignment operator are the same.

package com.yawintutor;

public class AssignmentMultiplyEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a *= b ;
		System.out.println("\n(a *= b)    : " + a);

		a = 6;
		a = a * b;
		System.out.println("\n(a = a * b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a *= b)    : 12

(a = a * b) : 12


Arithmetic Division Assignment Operator ( “/=” )

The following example shows the division of two numbers using arithmetic division operator and the shorthand “/=” assignment operator are the same.

package com.yawintutor;

public class AssignmentDivideEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a /= b ;
		System.out.println("\n(a /= b)    : " + a);

		a = 6;
		a = a / b;
		System.out.println("\n(a = a / b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a /= b)    : 3

(a = a / b) : 3


Arithmetic Mod Assignment Operator ( “%=” )

The following example shows the mod of two numbers using arithmetic mod operator and the shorthand “%=” assignment operator are the same.

package com.yawintutor;

public class AssignmentModEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a %= b ;
		System.out.println("\n(a %= b)    : " + a);

		a = 6;
		a = a % b;
		System.out.println("\n(a = a % b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a %= b)    : 0

(a = a % b) : 0


Bitwise AND Assignment Operator ( “&=” )

The following example shows the bitwise AND of two numbers using bitwise AND operator and the shorthand “&=” assignment operator are the same.

package com.yawintutor;

public class AssignmentANDEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a &= b ;
		System.out.println("\n(a &= b)    : " + a);

		a = 6;
		a = a & b;
		System.out.println("\n(a = a & b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a &= b)    : 2

(a = a & b) : 2


Bitwise OR Assignment Operator ( “|=” )

The following example shows the bitwise OR of two numbers using bitwise OR operator and the shorthand “|=” assignment operator are the same.

package com.yawintutor;

public class AssignmentOREqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a |= b ;
		System.out.println("\n(a |= b)    : " + a);

		a = 6;
		a = a | b;
		System.out.println("\n(a = a | b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a |= b)    : 6

(a = a | b) : 6


Bitwise XOR Assignment Operator ( “^=” )

The following example shows the bitwise XOR of two numbers using bitwise XOR operator and the shorthand “^=” assignment operator are the same.

package com.yawintutor;

public class AssignmentXOREqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a ^= b ;
		System.out.println("\n(a ^= b)    : " + a);

		a = 6;
		a = a ^ b;
		System.out.println("\n(a = a ^ b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a ^= b)    : 4

(a = a ^ b) : 4


Bitwise Left Shift Assignment Operator ( “<<=” )

The following example shows the left shift of two numbers using bitwise left shift operator and the shorthand “<<=” assignment operator are the same.

package com.yawintutor;

public class AssignmentLeftShiftEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a <<= b ;
		System.out.println("\n(a <<= b)    : " + a);

		a = 6;
		a = a << b;
		System.out.println("\n(a = a << b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a <<= b)    : 24

(a = a << b) : 24


Bitwise Right Shift Assignment Operator ( “>>=” )

The following example shows the right shift of two numbers using bitwise right shif operator and the shorthand “>>=” assignment operator are the same.

package com.yawintutor;

public class AssignmentRightShiftEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a >>= b ;
		System.out.println("\n(a >>= b)    : " + a);

		a = 6;
		a = a >> b;
		System.out.println("\n(a = a >> b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a >>= b)    : 1

(a = a >> b) : 1


Bitwise Unsigned Right Shift Assignment Operator ( “>>>=” )

The following example shows the unsigned right shift of two numbers using bitwise unsigned right shift operator and the shorthand “>>>=” assignment operator are the same.

package com.yawintutor;

public class AssignmentUnsignedRightShiftEqual {
	public static void main(String[] args) {
		int a = 6;
		int b = 2;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		a >>>= b ;
		System.out.println("\n(a >>>= b)    : " + a);

		a = 6;
		a = a >>> b;
		System.out.println("\n(a = a >>> b) : " + a);
	}
}

Output

a value is 6
b value is 2

(a >>>= b)    : 1

(a = a >>> b) : 1



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *