Operators in Scala
An operator is a symbol that represents an operation to be performed with one or more operand. Operators are the foundation of any programming language. Operators allow us to perform different kinds of operations on operands. There are different types of operators used in Scala as follows:
Arithmetic Operators
These are used to perform arithmetic/mathematical operations on operands.
- Addition(+) operator adds two operands. For example, x+y.
- Subtraction(-) operator subtracts two operands. For example, x-y.
- Multiplication(*) operator multiplies two operands. For example, x*y.
- Division(/) operator divides the first operand by the second. For example, x/y.
- Modulus(%) operator returns the remainder when the first operand is divided by the second. For example, x%y.
- Exponent(**) operator returns exponential(power) of the operands. For example, x**y.
Example:
Scala
// Scala program to demonstrate // the Arithmetic Operators object Arithop { def main(args : Array[String]) { // variables var a = 50 ; var b = 30 ; // Addition println( "Addition of a + b = " + (a + b)); // Subtraction println( "Subtraction of a - b = " + (a - b)); // Multiplication println( "Multiplication of a * b = " + (a * b)); // Division println( "Division of a / b = " + (a / b)); // Modulus println( "Modulus of a % b = " + (a % b)); } } |
Output:
Addition of a + b = 80 Subtraction of a - b = 20 Multiplication of a * b = 1500 Division of a / b = 1 Modulus of a % b = 20
Relational Operators
Relational operators or Comparison operators are used for comparison of two values. Let’s see them one by one:
- Equal To(==) operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
- Not Equal To(!=) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
- Greater Than(>) operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
- Less than(<) operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
- Greater Than Equal To(>=) operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
- Less Than Equal To(<=) operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.
Example:
Scala
// Scala program to demonstrate // the Relational Operators object Relop { def main(args : Array[String]) { // variables var a = 50 ; var b = 30 ; // Equal to operator println( "Equality of a == b is : " + (a == b)); // Not equal to operator println( "Not Equals of a != b is : " + (a ! = b)); // Greater than operator println( "Greater than of a > b is : " + (a > b)); // Lesser than operator println( "Lesser than of a < b is : " + (a < b)); // Greater than equal to operator println( "Greater than or Equal to of a >= b is : " + (a >= b)); // Lesser than equal to operator println( "Lesser than or Equal to of a <= b is : " + (a <= b)); } } |
Output:
Equality of a == b is : false Not Equals of a != b is : true Greater than of a > b is : true Lesser than of a = b is : true Lesser than or Equal to of a <= b is : false
Logical Operators
They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:
- Logical AND(&&) operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. Using “and” is an alternate for && operator. For example, a && b returns true when both a and b are true (i.e. non-zero).
- Logical OR(||) operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. Using “or” is an alternate for || operator. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
- Logical NOT(!) operator returns true the condition in consideration is not satisfied. Otherwise it returns false. Using “not” is an alternate for ! operator. For example, !true returns false.
Example:
Scala
// Scala program to demonstrate // the Logical Operators object Logop { def main(args : Array[String]) { // variables var a = false var b = true // logical NOT operator println( "Logical Not of !(a && b) = " + !(a && b)); // logical OR operator println( "Logical Or of a || b = " + (a || b)); // logical AND operator println( "Logical And of a && b = " + (a && b)); } } |
Output:
Logical Not of !(a && b) = true Logical Or of a || b = true Logical And of a && b = false
Assignment Operators
Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
- Simple Assignment (=) operator is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
- Add AND Assignment (+=) operator is used for adding left operand with right operand and then assigning it to variable on the left.
- Subtract AND Assignment (-=) operator is used for subtracting left operand with right operand and then assigning it to variable on the left.
- Multiply AND Assignment (*=) operator is used for multiplying the left operand with right operand and then assigning it to the variable on the left.
- Divide AND Assignment (/=) operator is used for dividing left operand with right operand and then assigning it to variable on the left.
- Modulus AND Assignment (%=) operator is used for assigning modulo of left operand with right operand and then assigning it to the variable on the left.
- Exponent AND Assignment (**=) operator is used for raising power of the left operand to the right operand and assigning it to the variable on the left.
- Left shift AND Assignment(<<=)operator is used to perform binary left shift of the left operand with the right operand and assigning it to the variable on the left.
- Right shift AND Assignment(>>=)operator is used to perform binary right shift of the left operand with the right operand and assigning it to the variable on the left.
- Bitwise AND Assignment(&=)operator is used to perform Bitwise And of the left operand with the right operand and assigning it to the variable on the left.
- Bitwise exclusive OR and Assignment(^=)operator is used to perform Bitwise exclusive OR of the left operand with the right operand and assigning it to the variable on the left.
- Bitwise inclusive OR and Assignment(|=)operator is used to perform Bitwise inclusive OR of the left operand with the right operand and assigning it to the variable on the left.
Example:
Scala
// Scala program to demonstrate // the Assignments Operators object Assignop { def main(args : Array[String]) { // variables var a = 50 ; var b = 40 ; var c = 0 ; // simple addition c = a + b; println( "simple addition: c= a + b = " + c); // Add AND assignment c + = a; println( "Add and assignment of c += a = " + c); // Subtract AND assignment c - = a; println( "Subtract and assignment of c -= a = " + c); // Multiply AND assignment c * = a; println( "Multiplication and assignment of c *= a = " + c); // Divide AND assignment c / = a; println( "Division and assignment of c /= a = " + c); // Modulus AND assignment c %= a; println( "Modulus and assignment of c %= a = " + c); // Left shift AND assignment c <<= 3 ; println( "Left shift and assignment of c <<= 3 = " + c); // Right shift AND assignment c >>= 3 ; println( "Right shift and assignment of c >>= 3 = " + c); // Bitwise AND assignment c & = a; println( "Bitwise And assignment of c &= 3 = " + c); // Bitwise exclusive OR and assignment c ^ = a; println( "Bitwise Xor and assignment of c ^= a = " + c); // Bitwise inclusive OR and assignment c | = a; println( "Bitwise Or and assignment of c |= a = " + c); } } |
Output:
simple addition: c= a + b = 90 Add and assignment of c += a = 140 Subtract and assignment of c -= a = 90 Multiplication and assignment of c *= a = 4500 Division and assignment of c /= a = 90 Modulus and assignment of c %= a = 40 Left shift and assignment of c <<= 3 = 320 Right shift and assignment of c >>= 3 = 40 Bitwise And assignment of c &= 3 = 32 Bitwise Xor and assignment of c ^= a = 18 Bitwise Or and assignment of c |= a = 50
Bitwise Operators
In Scala, there are 7 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators :
- Bitwise AND (&): Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- Bitwise OR (|): Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
- Bitwise XOR (^): Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- Bitwise left Shift (<<): Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
- Bitwise right Shift (>>): Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
- Bitwise ones Complement (~): This operator takes a single number and used to perform the complement operation of 8-bit.
- Bitwise shift right zero fill(>>>): In shift right zero fill operator, left operand is shifted right by the number of bits specified by the right operand, and the shifted values are filled up with zeros.
Example:
Scala
// Scala program to demonstrate // the Bitwise Operators object Bitop { def main(args : Array[String]) { // variables var a = 20 ; var b = 18 ; var c = 0 ; // Bitwise AND operator c = a & b; println( "Bitwise And of a & b = " + c); // Bitwise OR operator c = a | b; println( "Bitwise Or of a | b = " + c); // Bitwise XOR operator c = a ^ b; println( "Bitwise Xor of a ^ b = " + c); // Bitwise once complement operator c = ~a; println( "Bitwise Ones Complement of ~a = " + c); // Bitwise left shift operator c = a << 3 ; println( "Bitwise Left Shift of a << 3 = " + c); // Bitwise right shift operator c = a >> 3 ; println( "Bitwise Right Shift of a >> 3 = " + c); // Bitwise shift right zero fill operator c = a >>> 4 ; println( "Bitwise Shift Right a >>> 4 = " + c); } } |
Output:
Bitwise And of a & b = 16 Bitwise Or of a | b = 22 Bitwise Xor of a ^ b = 6 Bitwise Ones Complement of ~a = -21 Bitwise Left Shift of a << 3 = 160 Bitwise Right Shift of a >> 3 = 2 Bitwise Shift Right a >>> 4 = 1