Tutorials

Java Operators


Assignment Operator


In java, we have two types of assignment operator:

  • Assignment operator (=)
  • Compound assignment operator(+=, -=, *=, /=, %=)

We will read about each of them.

 

Assignment operator (=)

The assignment operator in java is used to assign (or to store) a value to a variable.

Example

a = 18; It assigns the value 18 to the variable a.

b = 8; It assigns the value 8 to the variable b.

 

a =b; It assigns the value of variable b to variable a.

Note: The value stored in variable a previously, if any, is lost. Now, both the variables will have value of b.

c = 5;*a; It assigns he value of variable a multiplied with 5 to variable c. the value of a does not change.

 

 

Compound Assignment Operators (+=, -=, *=, /=, %=)

Compound assignment operators are used to perform an operation with the value currently stored in a variable, and stores the new value in the same variable.

Given below is the chart showing compound operators in use:

 

Expression

Is equivalent to

value += d;

value = value +d;

a -=9;

a = a – 9;

a /= b;

a = a/b;

price*=units + 1000;

price = price* (units + 100);

A%=10;

A = A%10;

 



Please Share this page
Views : 31