Scala - Assignment Operators



Scala assignment operators are used to assign values to variables. In an assignment operation, the left-hand operand represents the variable receiving the value. Whereas, the right-hand operand denotes the value being assigned. Data type of the value on the right-hand side matches the data type of the variable on the left-hand side. Otherwise, the compiler will generate an error.

List of Scala Assignment Operators

There are following assignment operators supported by Scala language −

Operator Description Example
= Simple assignment operator, assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+= Add AND assignment operator, it adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, it subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, it multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, it divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, it takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Example of Scala Assignment Operators

Consider the following example program to understand all the Bitwise operators available in Scala programming language −

object Demo {
   def main(args: Array[String]) = {
      var a = 10;	
      var b = 20;
      var c = 0;

      c = a + b;
      println("c = a + b  = " + c );

      c += a ;
      println("c += a  = " + c );

      c -= a ;
      println("c -= a = " + c );

      c *= a ;
      println("c *= a = " + c );

      a = 10;
      c = 15;
      c /= a ;
      println("c /= a  = " + c );

      a = 10;
      c = 15;
      c %= a ;
      println("c %= a  = " + c );

      c <<= 2 ;
      println("c <<= 2  = " + c );

      c >>= 2 ;
      println("c >>= 2  = " + c );

      c >>= 2 ;
      println("c >>= 2  = " + c );

      c &= a ;
      println("c &= a  = " + c );
     
      c ^= a ;
      println("c ^= a  = " + c );

      c |= a ;
      println("c |= a  = " + c );
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

c = a + b  = 30
c += a  = 40
c -= a  = 30
c *= a  = 300
c /= a  = 1
c %= a  = 5
c <<= 2  = 20
c >>= 2  = 5
c >>= 2  = 1
c &= a  = 0
c ^= a  = 10
c |= a  = 10
Advertisements