๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Assignment Operators in Java  


Introduction

  • Assignment operators are used to perform an operation (like addition, subtraction, multiplication, etc.) and assign the result to a variable in a single step.
  • Assignment operators are explained as below :-
    • = (Assignment Operator):
      • Purpose: Assigns the right-hand operand value to the left-hand operand (variable).
      • Example: a = b assigns the value of b to a.
    • += (Addition Assignment Operator):
      • Purpose: Adds the right-hand operand value to the left-hand operand and assigns the result to the left operand.
      • Example: a += b is equivalent to a = a + b.
    • -= (Subtraction Assignment Operator):
      • Purpose: Subtracts the right-hand operand value from the left-hand operand and assigns the result to the left operand.
      • Example: a -= b is equivalent to a = a - b.
    • *= (Multiplication Assignment Operator):
      • Purpose: Multiplies the right-hand operand value by the left-hand operand and assigns the result to the left operand.
      • Example: a *= b is equivalent to a = a * b.
    • /= (Division Assignment Operator):
      • Purpose: Divides the left-hand operand by the right-hand operand and assigns the result to the left operand.
      • Example: a /= b is equivalent to a = a / b.
    • %= (Modulus Assignment Operator):
      • Purpose: Calculates the remainder of the division of the left operand by the right operand and assigns the result to the left operand.
      • Example: a %= b is equivalent to a = a % b.
  • Program :
    public class AssignmentOperators
    {
        public static void main(String[] args)
        {
            // Initialize variables
            int a = 10, b = 5;
            double x = 10.5, y = 4.2;
    
            // Simple Assignment (=)
            a = b;  // a is assigned the value of b (a = 5)
            System.out.println("Simple Assignment: a = " + a);  // Output: a = 5
    
            // Addition Assignment (+=)
            a += b;  // a = a + b (5 + 5)
            System.out.println("Addition Assignment: a = " + a);  // Output: a = 10
    
            // Subtraction Assignment (-=)
            a -= b;  // a = a - b (10 - 5)
            System.out.println("Subtraction Assignment: a = " + a);  // Output: a = 5
    
            // Multiplication Assignment (*=)
            a *= b;  // a = a * b (5 * 5)
            System.out.println("Multiplication Assignment: a = " + a);  // Output: a = 25
    
            // Division Assignment (/=)
            a /= b;  // a = a / b (25 / 5)
            System.out.println("Division Assignment: a = " + a);  // Output: a = 5
    
            // Modulus Assignment (%=)
            a %= b;  // a = a % b (5 % 5)
            System.out.println("Modulus Assignment: a = " + a);  // Output: a = 0
    
            // Using compound operators with double
            x += y;  // x = x + y (10.5 + 4.2)
            System.out.println("Addition Assignment (double): x = " + x);  // Output: x = 14.7
        }
    }
    Output:
    Simple Assignment: a = 5
    Addition Assignment: a = 10
    Subtraction Assignment: a = 5
    Multiplication Assignment: a = 25
    Division Assignment: a = 5
    Modulus Assignment: a = 0
    Addition Assignment (double): x = 14.7
Some Advance Important Points :-
  • Chained Assignment:
    • We can assign the same value to multiple variables in a single statement.
    • Example: a = b = c = 10; assigns 10 to a, b and c.
  • Implicit Casting with Compound Operators:
    • Compound operators (e.g., +=, *=) perform implicit type casting if needed.
    • Example: int a = 5; a += 2.5; results in a = 7 (2.5 is cast to an int).
  • String Concatenation:
    • The += operator concatenates strings instead of performing numeric addition.
    • Example: String s = "Java"; s += " Rocks"; results in "Java Rocks".
  • Final Variables:
    • Assignment operators cannot modify final variables.
    • Example: final int a = 10; a += 5; will cause a compilation error.
  • Overflow and Underflow:
    • For integer types, compound assignments can cause overflow or underflow.
    • Example: int a = Integer.MAX_VALUE; a += 1; wraps around to Integer.MIN_VALUE.
  • Precedence and Associativity:
    • Assignment operators have low precedence and are evaluated right-to-left.
    • Example: a = b = c = 10; evaluates as c = 10, then b = c, then a = b.
  • Side Effects:
    • Assignment operators modify the variable as part of the expression.
    • Example: int a = 5; int b = (a += 10) * 2; results in a = 15 and b = 30.
  • Not for Literals:
    • The left-hand operand of an assignment must be a variable, not a literal or constant.
    • Example: 5 += 3; is invalid.
  • Usage in Loops:
    • Compound assignment operators (+=, -=) are frequently used in loops to modify counters efficiently.
    • Example: for (int i = 0; i < 10; i += 2) iterates by steps of 2.
  • Cannot Chain Compound Operators:
    • Compound assignments like += cannot be directly chained.
    • Example: a += b += c; is invalid.
  • Works with All Primitives:
    • Assignment operators can be used with numeric types (int, float, etc.), char, and boolean where applicable.
  • Efficiency:
    • Compound operators are generally more efficient as they reduce the number of operations compared to their explicit counterparts.