R Assignment Operators In Java

An assignment operator assigns a value to its left operand based on the value of its right operand.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Overview

The basic assignment operator is equal (), which assigns the value of its right operand to its left operand. That is, assigns the value of to . The other assignment operators are usually shorthand for standard operations, as shown in the following definitions and examples.

Assignment

Simple assignment operator which assigns a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables. See the example.

Syntax

Operator: x = y

Examples

// Assuming the following variables // x = 5 // y = 10 // z = 25 x = y // x is 10 x = y = z // x, y and z are all 25

Addition assignment

The addition assignment operator adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible. See the addition operator for more details.

Syntax

Operator: x += y Meaning: x = x + y

Examples

// Assuming the following variables // foo = 'foo' // bar = 5 // baz = true // Number + Number -> addition bar += 2 // 7 // Boolean + Number -> addition baz += 1 // 2 // Boolean + Boolean -> addition baz += false // 1 // Number + String -> concatenation bar += 'foo' // "5foo" // String + Boolean -> concatenation foo += false // "foofalse" // String + String -> concatenation foo += 'bar' // "foobar"

Subtraction assignment

The subtraction assignment operator subtracts the value of the right operand from a variable and assigns the result to the variable. See the subtraction operator for more details.

Syntax

Operator: x -= y Meaning: x = x - y

Examples

// Assuming the following variable // bar = 5 bar -= 2 // 3 bar -= 'foo' // NaN

Multiplication assignment

The multiplication assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable. See the multiplication operator for more details.

Syntax

Operator: x *= y Meaning: x = x * y

Examples

// Assuming the following variable // bar = 5 bar *= 2 // 10 bar *= 'foo' // NaN

Division assignment

The division assignment operator divides a variable by the value of the right operand and assigns the result to the variable. See the division operator for more details.

Syntax

Operator: x /= y Meaning: x = x / y

Examples

// Assuming the following variable // bar = 5 bar /= 2 // 2.5 bar /= 'foo' // NaN bar /= 0 // Infinity

Remainder assignment

The remainder assignment operator divides a variable by the value of the right operand and assigns the remainder to the variable. See the remainder operator for more details.

Syntax

Operator: x %= y Meaning: x = x % y

Examples

// Assuming the following variable // bar = 5 bar %= 2 // 1 bar %= 'foo' // NaN bar %= 0 // NaN

Exponentiation assignment

This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

The exponentiation assignment operator evaluates to the result of raising first operand to the power second operand. See the exponentiation operator for more details.

Syntax

Operator: x **= y Meaning: x = x ** y

Examples

// Assuming the following variable // bar = 5 bar **= 2 // 25 bar **= 'foo' // NaN

Left shift assignment

The left shift assignment operator moves the specified amount of bits to the left and assigns the result to the variable. See the left shift operator for more details.

Syntax

Operator: x <<= y Meaning: x = x << y

Examples

var bar = 5; // (00000000000000000000000000000101) bar <<= 2; // 20 (00000000000000000000000000010100)

Right shift assignment

The right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the right shift operator for more details.

Syntax

Operator: x >>= y Meaning: x = x >> y

Examples

var bar = 5; // (00000000000000000000000000000101) bar >>= 2; // 1 (00000000000000000000000000000001) var bar -5; // (-00000000000000000000000000000101) bar >>= 2; // -2 (-00000000000000000000000000000010)

Unsigned right shift assignment

The unsigned right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the unsigned right shift operator for more details.

Syntax

Operator: x >>>= y Meaning: x = x >>> y

Examples

var bar = 5; // (00000000000000000000000000000101) bar >>>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>>= 2; // 1073741822 (00111111111111111111111111111110)

Bitwise AND assignment

The bitwise AND assignment operator uses the binary representation of both operands, does a bitwise AND operation on them and assigns the result to the variable. See the bitwise AND operator for more details.

Syntax

Operator: x &= y Meaning: x = x & y

Example

var bar = 5; // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 bar &= 2; // 0

Bitwise XOR assignment

The bitwise XOR assignment operator uses the binary representation of both operands, does a bitwise XOR operation on them and assigns the result to the variable. See the bitwise XOR operator for more details.

Syntax

Operator: x ^= y Meaning: x = x ^ y

Example

var bar = 5; bar ^= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111

Bitwise OR assignment

The bitwise OR assignment operator uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable. See the bitwise OR operator for more details.

Syntax

Operator: x |= y Meaning: x = x | y

Example

var bar = 5; bar |= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111

Examples

Left operand with another assignment operator

In unusual situations, the assignment operator (e.g.) is not identical to the meaning expression (here ). When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:

a[i++] += 5 // i is evaluated only once a[i++] = a[i++] + 5 // i is evaluated twice

Specifications

Browser compatibility

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Addition assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Bitwise AND assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Bitwise OR assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Bitwise XOR assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Division assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Exponentiation assignment ()Full support 52 ? Full support 52No support NoFull support Yes ? Full support 51Full support 52 ? Full support 52Full support Yes ? ? Full support Yes
Left shift assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Multiplication assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Remainder assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Right shift assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Subtraction assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Unsigned right shift assignment ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown

See also

Document Tags and Contributors

 Contributors to this page:wbamberg, stephaniehobson, fscholz, jameshkramer, nmve, kdex, torazaburo, samuele-artuso, io-ma, Sebastianz, JDurston, phylasnier, Havvy, Delapouite, Meghraj, Sheppy, trevorh, ethertank, Potappo, Mgjbot, Marcoos, Dria

 Last updated by:wbamberg,

Operators are special symbols (characters) that carry out operations on operands (variables and values). For example, is an operator that performs addition.

In Java variables article, you learned to declare variables and assign values to variables. Now, you will learn to use operators to manipulate variables.


Assignment Operator

Assignment operators are used in Java to assign values to variables. For example,

int age; age = 5;

The assignment operator assigns the value on its right to the variable on its left. Here, is assigned to the variable using = operator.

There are other assignment operators too. However, to keep things simple, we will learn other assignment operators later in this article.


Example 1: Assignment Operator

When you run the program, the output will be:

5 5

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

OperatorMeaning
+Addition (also used for string concatenation)
-Subtraction Operator
*Multiplication Operator
/Division Operator
%Remainder Operator

Example 2: Arithmetic Operator

When you run the program, the output will be:

In above example, all operands used are variables. However, it's not necessary at all. Operands used in arithmetic operators can be literals as well. For example,

result = number1 + 5.2; result = 2.3 + 4.5; number2 = number1 -2.9;

The operator can also be used to concatenate two or more strings.


Example 3: Arithmetic Operator

When you run the program, the output will be:


Unary Operators

Unary operator performs operation on only one operand.

OperatorMeaning
+Unary plus (not necessary to use since numbers are positive without using it)
-Unary minus; inverts the sign of an expression
++Increment operator; increments value by 1
--decrement operator; decrements value by 1
!Logical complement operator; inverts the value of a boolean

Example 4: Unary Operator

When you run the program, the output will be:


You can also use and -- operator as both prefix and postfix in Java. The operator increases value by 1 and operator decreases value by 1.

int myInt = 5; ++myInt // myInt becomes 6 myInt++ // myInt becomes 7 --myInt // myInt becomes 6 myInt-- // myInt becomes 5

Simple enough till now. However, there is a crucial difference while using increment and decrement operator as prefix and postfix. Consider this example,


Example 5: Unary Operator

When you run the program, the output will be:

When statement is executed, the original value is evaluated first. The is increased only after that. That's why you are getting as an output. Then, when is executed, the increased value 6.2 is displayed.

However, when is executed, is increased by 1 first before it's printed on the screen.

Similar is the case for decrement operator.


Equality and Relational Operators

The equality and relational operators determines the relationship between two operands. It checks if an operand is greater than, less than, equal to, not equal to and so on. Depending on the relationship, it results to either or .

OperatorDescriptionExample
==equal to5 == 3 is evaluated to
!=not equal to5 != 3 is evaluated to
>greater than5 > 3 is evaluated to
<less than5 < 3 is evaluated to
>=greater than or equal to5 >= 5 is evaluated to
<=less then or equal to5 <= 5 is evaluated to

Equality and relational operators are used in decision making and loops (which will be discussed later). For now, check this simple example.


Example 6: Equality and Relational Operators

When you run the program, the output will be:

Here, we have used operator to check if number1 is greater than number2 or not.

Since, is greater than , the expression > is evaluated to .

Hence, the block of code inside is executed and the block of code inside is skipped.

If you didn't understand the above code, don't worry. You will learn it in detail in Java if...else article.

For now, just remember that the equality and relational operators compares two operands and is evaluated to either or .


In addition to relational operators, there is also a type comparison operator which compares an object to a specified type. For example,


instanceof Operator

Here's an example of instanceof operator.

When you run the program, the output will be . It's because is the instance of class.

You will learn more about operator works once you understand Java Classes and Objects.


Logical Operators

The logical operators (conditional-OR) and (conditional-AND) operates on boolean expressions. Here's how they work.

OperatorDescriptionExample
||conditional-OR; if either of the boolean expression is is evaluated to
&&conditional-AND; true if all boolean expressions are is evaluated to false

Example 8: Logical Operators

When you run the program, the output will be:

Logical operators are used in decision making and looping.


Ternary Operator

The conditional operator or ternary operator is shorthand for  statement. The syntax of conditional operator is:

variable = Expression ? expression1 : expression2

Here's how it works.

  • If the is , is assigned to .
  • If the is , is assigned to .

Example 9: Ternary Operator

When you run the program, the output will be:

To learn more, visit Java ternary operator.


Bitwise and Bit Shift Operators

To perform bitwise and bit shift operators in Java, these operators are used.

OperatorDescription
~Bitwise Complement
<<Left Shift
>>Right Shift
>>>Unsigned Right Shift
&Bitwise AND
^Bitwise exclusive OR
|Bitwise inclusive OR

These operators are not commonly used. Visit this page to learn more about bitwise and bit shift operators.


More Assignment Operators

We have only discussed about one assignment operator  in the beginning of the article. Except this operator, there are quite a few assignment operators that helps us to write cleaner code.

OperatorExampleEquivalent to
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x / 5
<<=x <<= 5x = x << 5
>>=x >>= 5x = x >> 5
&=x &= 5x = x & 5
^=x ^= 5x = x ^ 5
|=x |= 5x = x | 5

Now you know about Java operators, it's time to learn precedence of Java operators; the order in which the operators in an expression are evaluated when two operators share a common operand.

0 thoughts on “R Assignment Operators In Java”

    -->

Leave a Comment

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