Java

Assignment Operators in Java | Explained

Programmers use operators to perform various operations on variables in their programs. The variables and values are also known as operands, and the operators manipulate the operands using any operator. Assignment operators in Java are used to assign new values to variables or the current values of variables can also be manipulated. There are multiple assignment operators that do the job in Java. This article provides an explanation of assignment operators used in Java:

So, let’s start

What are assignment operators in Java

This section provides the list of assignment operators that can be used in Java. Each operator is explained, and syntax is also provided that must be followed to use the assignment variables.

Before getting into the types, let’s have a look at the general syntax of assignment operators.

variable operator value;

Types of assignment operators

The assignment operators can be classified into the following types:

Single assignment operator

The “=” operator is used as a single assignment operator and the syntax to use this operator is provided below:

variable = value;

Compound assignment operator

The compound assignment operator comprises of two parts: one is the basic operator (“=”) and the other may be one of the (+, -, *, and /).

The following syntaxes may be used:

Syntax to use “=” and “–”

variable -= value;

Syntax to use “=” and “+”

variable += value;

Syntax to use “=” and “*”

variable *= value

Syntax to use “=” and “/”

variable /= value

After going through this section, you have learned the working mechanism of primary and various compound assignment operators.

How to use assignment operators in Java

As discussed earlier, the assignment operators may be used:

How to assign value to a variable using the simple assignment operator

As discussed earlier, a simple assignment operator is used to assign values to variables. For instance, the following statement assigns value 10 to variable a whose data type is int.

int a = 10;

We have used the above statement in the following code:

package lh;
public class oper {
    public static void main(String[]args) {
        // creating variable
        int a = 10;

        // printing value of a
        System.out.println("the value of a is:" + a);
    }
}

Output

Note: The data type of the value must be the same as that of the variable. As in the above example, we have used the int data type and the value also belongs to the integer category.

How to assign value to a variable using the compound assignment operators

Although this type also assigns a value to a variable, however, the assignment is performed after manipulating the existing value of a variable. To do so, the following possibilities may be considered:

Using “-” and “=”

The “-” belongs to the category of arithmetic operators and when it is joined with assignment operator “=”, it subtracts the current value of the variable with the value specified. For example, the following statement subtracts the current value of “b” by 5:

b -= 5;

The code provided below exercises the above statement:

package lh;
public class oper {
    public static void main(String[]args) {
        // creating variable
        int b = 12;

        // printing value of b
        System.out.println("current value of b is:" + b);
       
        //using "-=" operator
        b -= 5;
       
        //printing updated value of b
        System.out.println("new value of b is:" + b);
    }
}

The output of the above code is:

Using “+” and “=”

The “+” operator is used with “=” to add a specific value to the current value of a variable. Like we have added 7 to the current value of the variable named “c” using the statement provided below:

c += 7;

The above statement is used in the following Java code:

package lh;
public class oper {
    public static void main(String[]args) {
        // creating variable
        int c = 3;

        // printing value of b
        System.out.println("current value of c is:" + c);
       
        //using "+=" operator
        c += 7;
       
        //printing new value of c
        System.out.println("new value of c is:" + c);
    }
}

The output of the code is provided below:

Using “*” and “=”

Like + and -, the “*” operator is practiced in the following statement to multiply the current value of variable “d” with 2:

int d *= 3;

The code provided below practices the above statement:

package lh;
public class oper {
    public static void main(String[]args) {
        // creating variable
        int d = 5;

        // printing value of d
        System.out.println("current value of d is:" + d);
       
        //using "*=" operator
        d *= 3;
       
        //printing new value of d
        System.out.println("new value of d is:" + d);
    }
}

The output of the above code is provided below:

Using “/” and “=”

The following example provides the usage of “/” with “=”. The “/” and “=” operators manipulate the current value of a variable by dividing it with a specific value. For instance, the following statement divides the current value of a variable “e” by 2:

int e /= 2;

We used the above-mentioned statement in the following code:

package lh;
public class oper {
    public static void main(String[]args) {
        // creating variable
        int e = 6;

        // printing value of e
        System.out.println("current value of d is:" + e);
       
        //using "/=" operator
        e /= 2;
       
        //printing new value of e
        System.out.println("new value of d is:" + e);
    }
}

The output is displayed in the image below:

Using “%” and “=”

The arithmetic operator “%” is used to get the remainder. So, the following statement divides the current value of variable “f” by 2 and stores the remainder as a new value:

int f %= 3;

The code provided below shows the usage of the above statement:

package lh;
public class oper {
    public static void main(String[]args) {
        // creating variable
        int f = 13;

        // printing value of f
        System.out.println("current value of d is:" + f);
       
        //using "%=" operator
        f %= 3;
       
        //printing new value of f
        System.out.println("new value of d is:" + f);
    }
}

Output can be observed in the image shown below:

Conclusion

The assignment operators are practiced assigning values to variables. The primary assignment operator is “=”, whereas its functionality can be extended using “+, -, *, / and %” with the primary operator. This article demonstrates the usage of assignment operators in Java. You would learn the basic syntax and working mechanism of all assignment operators. Moreover, for better understandings, we have provided a few examples that would better clarify the usage of these operators.

About the author

Adnan Shabbir