In this article, we will discuss about built in Scala operators and operator notations.

Scala Operators Types and Notations

#1 Types of Scala Operators

Based on the operations, scala operators are divided into 5 types.

Arithmetic Operators perform arithmetic operations such as Add, Subtract, Multiply, Divide.

Example:

//file name:arithmetic.scala

val a=3

val b=2

//Addition

println(“a+b = ” +(a+b));

//Subtraction

println(“a-b = ” +(a-b));

//multiplication

println(“a*b = ” +(a*b));

//Division

println(“a/b = ” +(a/b));

//Modulus

println(“a%b = ” +(a%b))

We will get following output when running arithmetic.scala file.

scala> :load arithmetic.scala

Loading arithmetic.scala…

a: Int = 3

b: Int = 2

a+b = 5

a-b = 1

a*b = 6

a/b = 1

a%b = 1

Logical Operators perform logical operations (such as &&,||,!) on two variables.

Example:

//file name:Logic.scala

val t=true;

println(“t && !t = ” +(t && !t));

println(“t && t = ” +(t && t));

println(“t || t = ” +(t || t));

println(“t || !t = ” +(t || !t));

We will get following output when running logic.scala file.

scala> :load logic.scala

Loading logic.scala…

t: Boolean = true

// t=true , !t=false

//true && false=false

t && !t = false

//true && true =true

t && t = true

//true || true = true

t || t = true

//true || false = true

t || !t = true

Relational Operators perform comparison between operands and gives result as true if comparison is correct otherwise result is false.

Example:

// file name:relational.scala

val c=3 ;

val d=2;

//Equal to operator

println(“c==d = “+(c==d));

//Not Equal to operator

println(“c!=d = “+(c!=d));

//Greater than operator

println(“c>d = “+(c>d));

//Less than operator

println(“c<d = “+(c<d));

// Less than or equal to operator

println(“c<=d = “+(c<=d));

//Greater than or equal to operator

println(“c>=d = “+(c>=d));

We will get following output when running relational.scala file.

scala> :load relational.scala

Loading relational.scala…

c: Int = 3

d: Int = 2

c==d = false

c!=d = true

c>d = true

c<d = false

c<=d = false

c>=d = true

Bitwise Operators perform bit operations (such as AND , OR) on two variables.

Example:

//file name:bitwise.scala

//Binary AND

println(“2&3 = “+(2&3));

//Binary OR

println(“2|3 = “+(2|3));

//Binary XOR

println(“2^3 = “+(2^3));

//Binary One’s Complement

println(“~2 = “+(~2));

//Binary Shift Right

println(“-2 >> 31 = “+(-2 >> 31));

//Binary Unsigned Shift Right

println(“2 >>> 31 = “+(2 >>> 31));

//Binary Shift left

println(“2 << 3 = “+(2 << 3));

We will get following output when running bitwise.scala file.

scala> :load bitwise.scala

Loading bitwise.scala…

2&3 = 2

2|3 = 3

2^3 = 1

~2 = -3

-2 >> 31 = -1

2 >>> 31 = 0

2 << 3 = 16

There are 3 type of Bitwise shift operations: Shift Left (<<), Shift Right (>>), Unsigned Shift Right(>>>). Shift Right will fill the signed bit during each shift, but Shift left and Unsigned Shift Right will fill zeroes during each shift.

Assignment Operators areused to assign values to a Scala variable.

Example:

// file name: assignment.scala

var p = 15;

var q = 20;

var r = 0;

//simple assignment operator

r = p + q;

println(“r = p + q = ” + r );

//Add and assignment operator

r += p ;//r=r+p;

println(“r += p = ” + r );

//Subtract and assignment operator

r -= p ;//r=r-p;

println(“r -= p = ” + r );

//Multiply and assignment operator

r *= p ;//r=r*p;

println(“r *= p = ” + r );

//Divide and assignment operator

r /= p ;//r=r/p;

println(“r /= p = ” + r );

r = 20;

//Modulus and assignment operator

r %= p ;//r=r%p;

println(“r %= p = ” + r );

//Left shift operator

r <<= 2 ;//r=r<<2;

println(“r <<= 2 = ” + r );

//Right shift operator

r >>= 2;//r=r>>2;

println(“r >>= 2 = ” + r );

//Bitwise And and assignment operator

r &= p ;//r=r&p;

println(“r &= p = ” + r );

//Bitwise Exclusive OR and assignment operator

r ^= p ;//r=r^p;

println(“r ^= p = ” + r );

//Bitwise OR and assignment operator

r |= p ;//r=r|p;

println(“r |= p = ” + r );

We will get following output when running assignment.scala file

scala> :load assignment.scala

Loading assignment.scala…

p: Int = 15

q: Int = 20

r: Int = 0

r: Int = 35

r = p + q  = 35

r += p  = 50

r -= p = 35

r *= p = 525

r /= p  = 35

r: Int = 20

r %= p  = 5

r <<= 2 = 20

r >>= 2  = 5

r &= p  = 5

r ^= p  = 10

r |= p  = 15

#2 Operator Notations

There are 3 type of notations : prefix, infix, and postfix.

In prefix, an operator notation is placed before the an object.

Let us discuss with an example:

scala> val e = -3

e: Int = –3

In the above example ‘-‘ is an operator notation and ‘3’ is an object of Int class.

Scala is purely an object oriented language. Therefore every operator is a method of some class. When we write -3 , it means that scala will internally invoke unary_- method. Since 3 is an Int object, therefore unary_- is a method of Int class. Here ‘-‘ is notation for unary_- method.

We can also call method of prefix notations.

scala> val e = 3.unary_-

e: Int = –3

Infix notations are placed between two objects.

scala> val a=20

a: Int = 20

scala> val b=21

b: Int = 21

scala> val c=a+b

c: Int = 41

a  &  b  are  Int Objects and ‘+’ is method from Int class.  ‘+’ is also notation for ‘+’ method.

We can also rewrite the code as:

scala> val d=(a).+(b)

d: Int = 41

In the above code ‘+’ method takes b as input parameter.

Postfix notations are placed just after an Object.

scala> val p=21

p: Int = 21

scala> val q=p.toLong

q: Long = 21

In the above example, p is an Int object and toLong is method from Int class. toLong is notation for toLong method.

Conclusion

Operators are almost similar in all programming languages. We discussed following points about Scala operators:

  • Different types of operators
  • Example of each operator
  • Notations of operators
  • Examples of each notations

In this article, we will discuss about built in Scala operators and operator notations.

Scala Variable - Scala val vs Scala var - HadoopTree
Scala Variable – Scala val vs Scala var – HadoopTree

Scala Operators Types and Notations

#1 Types of Scala Operators

Based on the operations, scala operators are divided into 5 types.

Arithmetic Operators perform arithmetic operations such as Add, Subtract, Multiply, Divide.

Example:

//file name:arithmetic.scala

val a=3

val b=2

//Addition

println(“a+b = ” +(a+b));

//Subtraction

println(“a-b = ” +(a-b));

//multiplication

println(“a*b = ” +(a*b));

//Division

println(“a/b = ” +(a/b));

//Modulus

println(“a%b = ” +(a%b))

We will get following output when running arithmetic.scala file.

scala> :load arithmetic.scala

Loading arithmetic.scala…

a: Int = 3

b: Int = 2

a+b = 5

a-b = 1

a*b = 6

a/b = 1

a%b = 1

Logical Operators perform logical operations (such as &&,||,!) on two variables.

Example:

//file name:Logic.scala

val t=true;

println(“t && !t = ” +(t && !t));

println(“t && t = ” +(t && t));

println(“t || t = ” +(t || t));

println(“t || !t = ” +(t || !t));

We will get following output when running logic.scala file.

scala> :load logic.scala

Loading logic.scala…

t: Boolean = true

// t=true , !t=false

//true && false=false

t && !t = false

//true && true =true

t && t = true

//true || true = true

t || t = true

//true || false = true

t || !t = true

Relational Operators perform comparison between operands and gives result as true if comparison is correct otherwise result is false.

Example:

// file name:relational.scala

val c=3 ;

val d=2;

//Equal to operator

println(“c==d = “+(c==d));

//Not Equal to operator

println(“c!=d = “+(c!=d));

//Greater than operator

println(“c>d = “+(c>d));

//Less than operator

println(“c<d = “+(c<d));

// Less than or equal to operator

println(“c<=d = “+(c<=d));

//Greater than or equal to operator

println(“c>=d = “+(c>=d));

We will get following output when running relational.scala file.

scala> :load relational.scala

Loading relational.scala…

c: Int = 3

d: Int = 2

c==d = false

c!=d = true

c>d = true

c<d = false

c<=d = false

c>=d = true

Bitwise Operators perform bit operations (such as AND , OR) on two variables.

Example:

//file name:bitwise.scala

//Binary AND

println(“2&3 = “+(2&3));

//Binary OR

println(“2|3 = “+(2|3));

//Binary XOR

println(“2^3 = “+(2^3));

//Binary One’s Complement

println(“~2 = “+(~2));

//Binary Shift Right

println(“-2 >> 31 = “+(-2 >> 31));

//Binary Unsigned Shift Right

println(“2 >>> 31 = “+(2 >>> 31));

//Binary Shift left

println(“2 << 3 = “+(2 << 3));

We will get following output when running bitwise.scala file.

scala> :load bitwise.scala

Loading bitwise.scala…

2&3 = 2

2|3 = 3

2^3 = 1

~2 = -3

-2 >> 31 = -1

2 >>> 31 = 0

2 << 3 = 16

There are 3 type of Bitwise shift operations: Shift Left (<<), Shift Right (>>), Unsigned Shift Right(>>>). Shift Right will fill the signed bit during each shift, but Shift left and Unsigned Shift Right will fill zeroes during each shift.

Assignment Operators areused to assign values to a Scala variable.

Example:

// file name: assignment.scala

var p = 15;

var q = 20;

var r = 0;

//simple assignment operator

r = p + q;

println(“r = p + q = ” + r );

//Add and assignment operator

r += p ;//r=r+p;

println(“r += p = ” + r );

//Subtract and assignment operator

r -= p ;//r=r-p;

println(“r -= p = ” + r );

//Multiply and assignment operator

r *= p ;//r=r*p;

println(“r *= p = ” + r );

//Divide and assignment operator

r /= p ;//r=r/p;

println(“r /= p = ” + r );

r = 20;

//Modulus and assignment operator

r %= p ;//r=r%p;

println(“r %= p = ” + r );

//Left shift operator

r <<= 2 ;//r=r<<2;

println(“r <<= 2 = ” + r );

//Right shift operator

r >>= 2;//r=r>>2;

println(“r >>= 2 = ” + r );

//Bitwise And and assignment operator

r &= p ;//r=r&p;

println(“r &= p = ” + r );

//Bitwise Exclusive OR and assignment operator

r ^= p ;//r=r^p;

println(“r ^= p = ” + r );

//Bitwise OR and assignment operator

r |= p ;//r=r|p;

println(“r |= p = ” + r );

We will get following output when running assignment.scala file

scala> :load assignment.scala

Loading assignment.scala…

p: Int = 15

q: Int = 20

r: Int = 0

r: Int = 35

r = p + q  = 35

r += p  = 50

r -= p = 35

r *= p = 525

r /= p  = 35

r: Int = 20

r %= p  = 5

r <<= 2 = 20

r >>= 2  = 5

r &= p  = 5

r ^= p  = 10

r |= p  = 15

#2 Operator Notations

There are 3 type of notations : prefix, infix, and postfix.

In prefix, an operator notation is placed before the an object.

Let us discuss with an example:

scala> val e = -3

e: Int = –3

In the above example ‘-‘ is an operator notation and ‘3’ is an object of Int class.

Scala is purely an object oriented language. Therefore every operator is a method of some class. When we write -3 , it means that scala will internally invoke unary_- method. Since 3 is an Int object, therefore unary_- is a method of Int class. Here ‘-‘ is notation for unary_- method.

We can also call method of prefix notations.

scala> val e = 3.unary_-

e: Int = –3

Infix notations are placed between two objects.

scala> val a=20

a: Int = 20

scala> val b=21

b: Int = 21

scala> val c=a+b

c: Int = 41

a  &  b  are  Int Objects and ‘+’ is method from Int class.  ‘+’ is also notation for ‘+’ method.

We can also rewrite the code as:

scala> val d=(a).+(b)

d: Int = 41

In the above code ‘+’ method takes b as input parameter.

Postfix notations are placed just after an Object.

scala> val p=21

p: Int = 21

scala> val q=p.toLong

q: Long = 21

In the above example, p is an Int object and toLong is method from Int class. toLong is notation for toLong method.

Conclusion

Operators are almost similar in all programming languages. We discussed following points about Scala operators:

  • Different types of operators
  • Example of each operator
  • Notations of operators
  • Examples of each notations