Assignment Operators

Java Assignment operators

An Assignment Operator is an operator used to assign a new value to a variable. The assigning operator in Java is marked with the symbol “=”. It is employed for assigning a value to a variable. The basic purpose of the assignment operator is to store the result of an expression or a constant value into a variable, allowing you to use and manipulate that value later in the program.
variable = expression or value;

Example:

public class AssignmentOperatorExample {
    public static void main(String[] args) {
        int x; // Declare a variable 'x' of type int
        x = 10; // Assign the value 10 to the variable 'x'

        System.out.println("The value of x is: " + x);
    }
}

In this example, we declared an integer variable ‘x’, assigned the value 10 to it using the assignment operator (=), and then printed its value to the console.

It’s important to understand that the assignment operator does not check for equality (like the equality operator ==). Instead, it takes the value on the right and assigns it to the variable on the left. For example, x = 10; assigns the value 10 to the variable ‘x’, while x == 10; checks whether the value of ‘x’ is equal to 10.

The assignment operator can also be combined with other operators to perform arithmetic and bitwise assignments, like +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, and >>>=. The combined assignment operators in Java allow you to perform arithmetic and bitwise operations on a variable and assign the result back to the same variable in a more concise manner. They combine the basic assignment operator “=” with other arithmetic or bitwise operators, making the code more compact and readable.

 
Operator                                                 Description Example
= Assigns values from right side operands to left side operand c = a + b
+= It adds right operand to the left operand and assigns the result to left operand c += a
-= It subtracts right operand from the left operand and assigns the result to left operand c -= a
*= It multiplies right operand with the left operand and assigns the result to left operand c *= a
/= It divides left operand with the right operand and assigns the result to left operand c /= a
%= It takes modulus using two operands and assigns the result to left operand c %= a
^= Performs exponential (power) calculation on operators and assign value to the left operand c ^= a

Example:

public class AssignmentOperatorsExample {
        public static void main(String[] args) {
            int a = 10;
            int b = 20;
            int c;
            
            // Assignment Operator
            c = a;
            System.out.println("c = " + c); // Output: c = 10

            // Addition Assignment Operator
            b += a;
            System.out.println("b += a: " + b); // Output: b += a: 30

            // Subtraction Assignment Operator
            b -= a;
            System.out.println("b -= a: " + b); // Output: b -= a: 20

            // Multiplication Assignment Operator
            b *= a;
            System.out.println("b *= a: " + b); // Output: b *= a: 200

            // Division Assignment Operator
            b /= a;
            System.out.println("b /= a: " + b); // Output: b /= a: 20

            // Modulus Assignment Operator
            b %= a;
            System.out.println("b %= a: " + b); // Output: b %= a: 0

            // Bitwise AND Assignment Operator
            b = 12; // In binary: 1100
            b &= 3; // In binary: 0011
            System.out.println("b &= 3: " + b); // Output: b &= 3: 0

            // Bitwise OR Assignment Operator
            b = 9; // In binary: 1001
            b |= 6; // In binary: 0110
            System.out.println("b |= 6: " + b); // Output: b |= 6: 15

            // Bitwise XOR Assignment Operator
            b = 10; // In binary: 1010
            b ^= 3; // In binary: 0011
            System.out.println("b ^= 3: " + b); // Output: b ^= 3: 9
        }
    }

Output:

c = 10

b += a: 30

b -= a: 20

b *= a: 200

b /= a: 20

b %= a: 0

b &= 3: 0

b |= 6: 15

b ^= 3: 9

   

In this example, the program demonstrates various assignment operators and their usage:

=: The basic assignment operator, used to assign a value to a variable.

+=: Adds a value to a variable and stores the result in the same variable.

-=: Subtracts a value from a variable and stores the result in the same variable.

*=: Multiplies a variable by a value and stores the result in the same variable.

/=: Divides a variable by a value and stores the result in the same variable.

%=: Calculates the remainder of dividing a variable by a value and stores it in the same variable.

&=: Performs bitwise AND between a variable and a value, storing the result in the same variable.

|=: Performs bitwise OR between a variable and a value, storing the result in the same variable.

^=: Performs bitwise XOR between a variable and a value, storing the result in the same variable.

Each assignment operator offers a convenient way to modify variables in a single line.

 





Visited 1 times, 1 visit(s) today

Comments are closed.

Close