No announcement
 

Assignment Operators

An assignment operator is used to assign value to a variable. Also, we can combine assignment operator to a binary operator as a short hand notion.

Example:

AssignmentOperatorsDemo.java

package com.codingnous.tutorial;

public class AssignmentOperatorsDemo {

	public static void main(String[] args) {

		// Two values for performing the arithmetic operations.
		int num1 = 100;
		int num2 = 22;

		System.out.println("num1 = " + num1 + " and num2 = " + num2);
		// Addition operation, Add num1 and num2 and store in num1.
		num1 += num2;
		System.out.println("Addition = " + num1);

		System.out.println("num1 = " + num1 + " and num2 = " + num2);
		// Subtraction operation, Subtract num1 and num2 and store in num1.
		num1 -= num2;
		System.out.println("Subtraction = " + num1);

		System.out.println("num1 = " + num1 + " and num2 = " + num2);
		// Multiplication operation, Multiply num1 and num2 and store in num1.
		num1 *= num2;
		System.out.println("Multiplication = " + num1);
	}

}

Output
num1 = 100 and num2 = 22 Addition = 122 num1 = 122 and num2 = 22 Subtraction = 100 num1 = 100 and num2 = 22 Multiplication = 2200