Java Assignment Operators Tutorial

In this section we will learn what assignment operators are and how they work in Java.

Note: we’re assuming you’re familiar with Java operators in general and Java variables.  

What Is Assignment Operator in Java?

When we want to store a value in a variable we use assignment operators.  

Example:

int iVar = 10;

The `=` symbol is called assignment operator and is used to assign a value (in this case 10) to a variable.

List of assignment operators in Java

In the table below you can see the rest of assignment operators:

OperatorSymbol
Assignment operator=
Addition and Assignment operator+=
Subtract and Assignment operator-=
Multiplication and Assignment operator*=
Division and Assignment operator/=

Java Assignment `=` Operator:

As mentioned at the beginning of this section, this operator is used to assign a value to a variable. The important note to remember is that, this operator will replace the current content of the variable (if any) with the new value.

Assignment `=` Operator Syntax:

variable = value; 

Assignment `=` Operator Example:

double dVar = 2.033; 
dVar = 1000.32;

In this example, the `dVar` variable is initialized with the value `2.033` via the assignment operator and then we replaced the current value of the variable with the value `1000.32` again via assignment operator. 

Java Addition and Assignment `+=` Operator:

This is a compound operator which is basically the combination of addition `+` and assignment `=` operators.

What it does is that, it will select the value that is already stored in the memory space of the target variable and add it to the new value and after that, stores the result in the variable itself.

Addition and Assignment `+=` Operator Syntax:

variable += value;

Addition and Assignment `+=` Operator Example:

int vary = 100; 
vary += 200; 

Here the current value of the variable named vary is 100 but then we used the += operator and assigned the value 200. So as a result, the current value (100) is added to the value (200) and the final result is stored in the vary variable. (The final value that is stored in this variable is 300).

Note: the example above could be rewritten like this:

int vary = 100; 
vary =  vary + 200; 

Java Subtract and Assignment -= Operator:

If we want to subtract a value of a variable from another value and store the result in the variable itself, this operator is the way to go.

Subtract and Assignment -= Operator Syntax:

variable -= value;

Subtract and Assignment -= Operator Example:

int vary = 50; 
vary -= 30; 

The example above could be rewritten like this:

int vary = 50; 
vary = vary - 30;

The final value stored in the vary variable for both of the examples above is 20.

Java Multiplication and Assignment *= Operator:

This operator is used to multiply the current value stored in a variable to another value and store the result of the multiplication in the variable itself.

Multiplication and Assignment *= Operator Syntax:

variable *= value;

Multiplication and Assignment *= Operator Example:

int multiply  = 10; 
multiply *= 20;

The final value that will be stored in the multiply variable is 200.

What happened is that via *= operator, we multiplied the value stored in the multiply variable which is 10 to the number 20 and assigned the result to the multiply variable.

The example above could be rewritten like this:

int multiply  = 10; 
multiply =  multiply *20; 

The result of this example is also 200.

Java Division and Assignment /= Operator:

If we want to quickly divide the value of a variable by another value and assign the result to the variable itself, the /= operator is the perfect candidate.

Division and Assignment /= Operator Syntax:

variable /= value;

Division and Assignment /= Operator Example:

int division  = 100; 
division /= 20; 

The final value that will be stored in the division variable is 5.

What happened is that via /= operator, we divided the value stored in the division variable which is 100 by the number 20 and assigned the result to the division variable.

The example above could be rewritten like this:

int division  = 100; 
division =  division /20; 

The result of this example is also 5.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies