Java Assignment Operators

Java assignment operators are used to assign a value to a variable. The basic assignment operator in Java is the equals sign =, which is used to assign a value to a variable.

int a = 10; // assigns the value 10 to the variable a
Sour‮i.www:ec‬giftidea.com

There are also compound assignment operators in Java that combine an arithmetic operation with an assignment operation. These operators perform the arithmetic operation first, and then assign the result to the variable.

Here are the compound assignment operators in Java:

  1. Addition assignment: +=
    The addition assignment operator adds a value to a variable and then assigns the result to the variable.
int a = 10;
a += 5; // equivalent to a = a + 5;

After this operation, the value of a will be 15.

  1. Subtraction assignment: -=
    The subtraction assignment operator subtracts a value from a variable and then assigns the result to the variable.
int a = 10;
a -= 5; // equivalent to a = a - 5;

After this operation, the value of a will be 5.

  1. Multiplication assignment: *=
    The multiplication assignment operator multiplies a variable by a value and then assigns the result to the variable.
int a = 10;
a *= 5; // equivalent to a = a * 5;

After this operation, the value of a will be 50.

  1. Division assignment: /=
    The division assignment operator divides a variable by a value and then assigns the result to the variable.
int a = 10;
a /= 5; // equivalent to a = a / 5;

After this operation, the value of a will be 2.

  1. Modulus assignment: %=
    The modulus assignment operator calculates the remainder of a variable divided by a value, and then assigns the result to the variable.
int a = 10;
a %= 3; // equivalent to a = a % 3;

After this operation, the value of a will be 1.