Assignment operators in java with examples w3schools

Stella 160 Published: 08/25/2024

Assignment operators in java with examples w3schools

I'm happy to help you with that!

Assignment Operators in Java:

In Java, assignment operators are used to assign values to variables. There are several types of assignment operators in Java, each having its own specific use case and functionality.

Assignment Operator (=):

The simplest and most basic assignment operator is the equal sign (=). It assigns a value to a variable.

Example:

int x = 10;

In this example, the value 10 is assigned to the integer variable x.

Addition Assignment Operator (+=):

The addition assignment operator (+=) adds a value to the current value of a variable and assigns it back to the same variable.

Example:

int x = 5;

x += 3; // equivalent to x = x + 3;

In this example, the value 3 is added to the current value of x (which is 5), resulting in a new value of 8.

Subtraction Assignment Operator (-=):

The subtraction assignment operator (-=) subtracts a value from the current value of a variable and assigns it back to the same variable.

Example:

int x = 10;

x -= 3; // equivalent to x = x - 3;

In this example, the value 3 is subtracted from the current value of x (which is 10), resulting in a new value of 7.

Multiplication Assignment Operator (*=):

The multiplication assignment operator (*=) multiplies a value by the current value of a variable and assigns it back to the same variable.

Example:

int x = 5;

x *= 2; // equivalent to x = x * 2;

In this example, the value 2 is multiplied by the current value of x (which is 5), resulting in a new value of 10.

Division Assignment Operator (/=):

The division assignment operator (/=) divides a value by the current value of a variable and assigns it back to the same variable.

Example:

double x = 10.0;

x /= 2; // equivalent to x = x / 2;

In this example, the value 2 is divided by the current value of x (which is 10), resulting in a new value of 5.

Modulus Assignment Operator (%=):

The modulus assignment operator (%=) takes the remainder of a division operation and assigns it back to the same variable.

Example:

int x = 17;

x %= 5; // equivalent to x = x % 5;

In this example, the remainder of 17 divided by 5 is calculated (which is 2), resulting in a new value of 2.

Bitwise AND Assignment Operator (&=):

The bitwise AND assignment operator (&=) performs a bitwise AND operation on two binary values and assigns it back to the same variable.

Example:

int x = 5;

x &= 3; // equivalent to x = x & 3;

In this example, the bitwise AND operation is performed between the binary value of x (which is 101) and the binary value of 3 (which is 011), resulting in a new binary value of 001.

Bitwise OR Assignment Operator(|=):

The bitwise OR assignment operator (|=) performs a bitwise OR operation on two binary values and assigns it back to the same variable.

Example:

int x = 5;

x |= 3; // equivalent to x = x | 3;

In this example, the bitwise OR operation is performed between the binary value of x (which is 101) and the binary value of 3 (which is 011), resulting in a new binary value of 111.

Bitwise XOR Assignment Operator (^=):

The bitwise XOR assignment operator (^=) performs a bitwise XOR operation on two binary values and assigns it back to the same variable.

Example:

int x = 5;

x ^= 3; // equivalent to x = x ^ 3;

In this example, the bitwise XOR operation is performed between the binary value of x (which is 101) and the binary value of 3 (which is 011), resulting in a new binary value of 100.

Left Shift Assignment Operator (<<=):

The left shift assignment operator (<<=) shifts the bits of a value to the left by a specified number of positions.

Example:

int x = 5;

x <<= 2; // equivalent to x = x << 2;

In this example, the bits of x are shifted two positions to the left, resulting in a new value of 20.

Right Shift Assignment Operator (>>=):

The right shift assignment operator (>>=) shifts the bits of a value to the right by a specified number of positions.

Example:

int x = 5;

x >>= 2; // equivalent to x = x >> 2;

In this example, the bits of x are shifted two positions to the right, resulting in a new value of 1.

These assignment operators can be used to simplify and shorten your code, making it easier to read and maintain.

List of assignment operators in java with examples

Here are the list of assignment operators in Java along with their descriptions and examples:

= (Assignment Operator): This operator assigns a value to a variable. For example:

int x = 5; // declares an integer variable x and initializes it to 5

+= (Addition Assignment Operator): This operator adds the right-hand side value to the left-hand side variable and stores the result in the variable. For example:
int x = 5;

x += 3; // x will be assigned 8

System.out.println(x); // prints 8

-= (Subtraction Assignment Operator): This operator subtracts the right-hand side value from the left-hand side variable and stores the result in the variable. For example:
int x = 5;

x -= 2; // x will be assigned 3

System.out.println(x); // prints 3

*=(Multiplication Assignment Operator): This operator multiplies the left-hand side variable with the right-hand side value and stores the result in the variable. For example:
int x = 5;

x *= 2; // x will be assigned 10

System.out.println(x); // prints 10

/=(Division Assignment Operator): This operator divides the left-hand side variable by the right-hand side value and stores the result in the variable. For example:
int x = 8;

x /= 2; // x will be assigned 4

System.out.println(x); // prints 4

%=(Modulus Assignment Operator): This operator calculates the remainder of dividing the left-hand side variable by the right-hand side value and stores the result in the variable. For example:
int x = 11;

x %= 3; // x will be assigned 2

System.out.println(x); // prints 2

~=(Bitwise NOT Assignment Operator): This operator performs a bitwise NOT operation on the left-hand side variable and stores the result in the variable. For example:
int x = 5;

x =~ 1; // x will be assigned -6

System.out.println(x); // prints -6

&=(Bitwise AND Assignment Operator): This operator performs a bitwise AND operation on the left-hand side variable with the right-hand side value and stores the result in the variable. For example:
int x = 5;

x &= 3; // x will be assigned 1

System.out.println(x); // prints 1

^=(Bitwise XOR Assignment Operator): This operator performs a bitwise XOR operation on the left-hand side variable with the right-hand side value and stores the result in the variable. For example:
int x = 5;

x ^= 3; // x will be assigned 6

System.out.println(x); // prints 6

|(Bitwise OR Assignment Operator): This operator performs a bitwise OR operation on the left-hand side variable with the right-hand side value and stores the result in the variable. For example:
int x = 5;

x |= 3; // x will be assigned 7

System.out.println(x); // prints 7

These are all the assignment operators in Java, along with their descriptions and examples.