Select Page

Java Assignment Operators | Developer.com

Robert Gravelle
Published: December 19, 2022

Java Programming tutorials

Java provides many types of operators to perform a variety of calculations and functions, such as logical, arithmetic, relational, and others. With so many operators to choose from, it helps to group them based on the type of functionality they provide. This programming tutorial will focus on Java’s numerous assignment operators.

Before we begin, however, you may want to bookmark our other tutorials on Java operators, which include:

Assignment Operators in Java

As the name conveys, assignment operators are used to assign values to a variable using the following syntax:

variable operator value;

The left side operand of the assignment operator must be a variable, whereas the right side operand of the assignment operator may be a literal value or another variable. Moreover, the value or variable on the right side must be of the same data type of the operand on the left side. Otherwise, the compiler will raise an error. Assignment operators have a right to left associativity in that the value given on the right-hand side of the operator is assigned to the variable on the left. Therefore, the right-hand side variable must be declared before assignment.

You can learn more about variables in our programming tutorial: Working with Java Variables.

Types of Assignment Operators in Java

Java assignment operators are classified into two types: simple and compound.

The Simple assignment operator is the equals (=) sign, which is the most straightforward of the bunch. It simply assigns the value or variable on the right to the variable on the left.

Compound operators are comprised of both an arithmetic, bitwise, or shift operator in addition to the equals (=) sign.

Equals Operator (=) Java Example

First, let’s learn to use the one-and-only simple assignment operator – the Equals (=) operator – with the help of a Java program. It includes two assignments: a literal value to num1 and the num1 variable to num2, after which both are printed to the console to show that the values have been assigned to the numbers:

class EqualsOperatorExample {
  public static void main(String[] args) {
    int num1, num2;
    // Assigning a literal value to num1
    num1 = 5;
    System.out.println(num1); // 5
    // Assigning value of variable num1 (5) to num2
    num2 = num1;
    System.out.println(num2); // 5
  }
}

The += Operator Java Example

A compound of the + and = operators, the += adds the current value of the variable on the left to the value on the right before assigning the result to the operand on the left. Here is some sample code to demonstrate how to use the += operator in Java:

class CompoundAdditionOperatorExample {
  public static void main(String[] args)
  {
    int num1 = 20, num2 = 30;
        
    System.out.println("num1 = " + num1); // num1 = 20
    System.out.println("num2 = " + num2); // num2 = 30
    
    // Adding and Assigning values
    num1 += num2;
    
    System.out.println("num1 = " + num1); // num1 = 50
  }
}

The -= Operator Java Example

Made up of the and = operators, the -= first subtracts the variable’s value on the right from the current value of the variable on the left before assigning the result to the operand on the left. We can see it at work below in the following code example showing how to decrement in Java using the -= operator:

class CompoundSubtractionOperatorExample {
  public static void main(String[] args)
  {
    int num1 = 20, num2 = 30;
    
    System.out.println("num1 = " + num1); // num1 = 20 
    System.out.println("num2 = " + num2); // num2 = 30
    
    // Subtracting and Assigning values
    num1 -= num2;

    System.out.println("num1 = " + num1); // num1 = -10
  }
}

The *= Operator Java Example

This Java operator is comprised of the * and = operators. It operates by multiplying the current value of the variable on the left to the value on the right and then assigning the result to the operand on the left. Here’s a program that shows the *= operator in action:

class CompoundMultiplicationOperatorExample {
  public static void main(String[] args)
  {
    int num1 = 20, num2 = 30;
    
    System.out.println("num1 = " + num1); // num1 = 20 
    System.out.println("num2 = " + num2); // num2 = 30
    
    // Multiplying and Assigning values
    num1 *= num2;
 
    // Displaying the assigned values
    System.out.println("num1 = " + num1); // num1 = 600
  }
}

The /= Operator Java Example

A combination of the / and = operators, the /= Operator divides the current value of the variable on the left by the value on the right and then assigns the quotient to the operand on the left. Here is some example code showing how to use the /= operator in Java:

class CompoundDivisionOperatorExample {
  public static void main(String[] args)
  {
    int num1 = 30, num2 = 20;
    
    System.out.println("num1 = " + num1); // num1 = 30 
    System.out.println("num2 = " + num2); // num2 = 20
    
    // Multiplying and Assigning values
    num1 /= num2;
    
    // Displaying the assigned values
    System.out.println("num1 = " + num1); // num1 = 1
  }
}

%= Operator Java Example

The %= operator includes both the % and = operators. As seen in the program below, it divides the current value of the variable on the left by the value on the right and then assigns the remainder to the operand on the left:

class CompoundModulusOperatorExample {
  public static void main(String[] args)
  {
    int num1 = 30, num2 = 20;
    
    System.out.println("num1 = " + num1); // num1 = 30 
    System.out.println("num2 = " + num2); // num2 = 20
    
    // Modulus and value assignment
    num1 %= num2;
    
    // Displaying the assigned values
    System.out.println("num1 = " + num1); // num1 = 10
  }
}

Compound Bitwise and Shift Operators in Java

The Bitwise and Shift Operators that we just recently covered can also be utilized in compound form as seen in the list below:

  • &= – Compound bitwise Assignment operator.
  • ^= – Compound bitwise ^ assignment operator.
  • >>= – Compound right shift assignment operator.
  • >>>= – Compound right shift filled 0 assignment operator.
  • <<= – Compound left shift assignment operator.

The following program demonstrates the working of all the Compound Bitwise and Shift Operators:

class CompoundBitwiseAndShiftOperatorsExample {
  public static void main(String args[]) {
    byte b1 = 127;
    b1 %= 7;
    byte b2 = 120;
    b2 &= 40;
    short s1 = 300;
    s1 ^= 100;
    byte b3 = 127;
    b3 >>= 3;
    short s2 = 100;
    s2 >>= 3;
    short s3 = 200;
    s3 >>>= 4;
    System.out.println("b1 = " + b1); // b1 = 1
    System.out.println("b2 = " + b2); // b2 = 40
    System.out.println("b3 = " + b3); // b3 = 15
    System.out.println("s1 = " + s1); // s1 = 328
    System.out.println("s2 = " + s2); // s2 = 800
    System.out.println("s3 = " + s3); // s3 = 12
  }
}

Final Thoughts on Java Assignment Operators

This programming tutorial presented an overview of Java’s simple and compound assignment Operators. An essential building block to any programming language, developers would be unable to store any data in their programs without them. Though not quite as indispensable as the equals operator, compound operators are great time savers, allowing you to perform arithmetic and bitwise operations and assignment in a single line of code.

Read more Java programming tutorials and guides to software development.

Source: www.developer.com