Operators in Scala (original) (raw)

Last Updated : 31 Aug, 2021

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.

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:

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:

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:

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 :

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