Trending September 2023 # Learn 5 Major Types Of Scala Operators # Suggested October 2023 # Top 16 Popular | Happystarlongbien.com

Trending September 2023 # Learn 5 Major Types Of Scala Operators # Suggested October 2023 # Top 16 Popular

You are reading the article Learn 5 Major Types Of Scala Operators updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn 5 Major Types Of Scala Operators

Introduction to Scala Operators

Operators are used to perform logical and mathematical computation in any programming language. Scala also has various operators to perform various calculations and tasks but they are implemented as methods as Scala is an object-oriented language hence it treats every symbol as object and operation as a method. They make computation simple and easy.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Different operators present in Scala are:

Arithmetic operators

Assignment operators

Relational operators

Logical operators

Bitwise operators

Now let us study each operator in detail.

Scala Arithmetic Operators

These operators are used to perform mathematical calculations or computations.

Operator Symbol Explanation Format

Addition + Adds both operands x + y

Subtraction – Subtracts right operand from the left one x – y

Multiplication * Multiplies both the operands x * y

Division / Divide numerator by the denominator x / y

Modulus % Returns remainder after division x % y

Example: Arithmetic Operators in Scala

Code:

object Arith { def main (args: Array [String]) { var a = 10; var b = 5; println (a + b); println (a – b); println (a * b); println (a / b); println (a % b) } }

15

5

50

2

0

Scala Assignment Operators

These operators are used to assign values to a variable or an object.

Operator Symbol Explanation Format

Assignment = Assigns the value of right operand to the left operand x = y + z

Addition += Adds both operands and finally assign the value to the left operand x += y

Subtraction -= Subtracts right operand from the left and then assign the value to the left operand x -= y

Multiplication *= Multiplies both operands and assign the value to the left operand x *= y

Division /= Divides left operand by the right operand and assign the value to the left operand x /= y

Modulus %= Evaluates modulus of two operands and assign the value to the left operand x %= y

Bitwise AND &= Compares the binary value of two operands, return 1 if both operands are 1 else return 0 and assign the value to the left operand x &= 5

Bitwise OR |= Compares the binary value of two operands, return 0 if both operands are 0 else return 1 and assign the value to the left operand

Bitwise XOR ^= Compares the binary value of two operands, return 0 if both operands are same else return 1 and assign the value to the left operand x ^= 5

Left shift <<= Shifts the bits towards left and assign the result to the left operand x <<= 5

Right shift >>= Shifts the bits towards the right and assign the result to the left operand

Example: Assignment operators in Scala

Code:

object Assign { def main (args: Array [String]) { var a = 10; var b = 5; println (a += b); println (a –= b); println (a *= b); println (a /= b); println (a %= b); a = 20; b = 15; println (a &= b); println (a ^= b); println (a <<= 2); } }

15

10

50

10

0

4

11

4

16

4

Scala Relational Operators

These operators return Boolean value after checking the mentioned conditions.

Operator Symbol Explanation Format

Equal to == Returns true if both operands are equal else return false x == y

Not Equal to != Returns true if both operands are not equal else return false x != y

Greater than > Returns true if the left operand is greater than right else return false

Less than < Returns true if the left operand is smaller than right else return false x < y

Greater than or equal to >= Returns true if the left operand is greater than or equal to the right else return false

Less than or equal to <= Returns true if the left operand is smaller than or equal to the right else return false x <= y

Code:

object Relation { def main (args: Array [String]) { var a = 10; var b = 5; println (a == b); println (a != b); println (a < b); println (a <= b); } }

Output:

false

true

true

false

true

false

Scala Logical Operator

These operators also return Boolean value according to the inputs or operands.

Operator Symbol Explanation Format

Logical AND && Returns true if both operands are non zero else return false x && y

Logical OR || Returns true if any of the operands is nonzero else return false

Logical NOT ! It reverses the operand. Returns true for false and vice versa !x

Example: Logical operators in Scala

Code:

object Logic { def main (args: Array [String]) { var a = true; var b = false; println (a && b); println !(b); } }

Output:

false

true

true

Scala Bitwise Operators

These operators work on bits and return corresponding integer value as output.

Operator Symbol Explanation Format

Binary AND & Check the operands bitwise and return 1 if both bits are 1 else return 0 x & y

Binary OR | Check the operands bitwise and return 0 if both bits are 0 else return 1

Binary XOR ^ Check the operands bitwise and return 0 if both bits are same else return 1 x ^ y

Binary NOT ~ Returns ones complement i.e. changes 1 to 0 and vice versa ~x

Binary Left Shift << Bits of the left operand is shifted left side by the number of bits mentioned by the right operand x << 3

Binary Right Shift >> Bits of the left operand is shifted right side by the number of bits mentioned by the right operand

Binary Right Shift zero fill >>> Bits of the left operand is shifted right side by the number of bits mentioned by right operand and shifted values are substituted bu zeroes.

Example: Bitwise Operators in Scala

Code:

object Bit { def main (args: Array [String]) { var a = 10; var b = 5; println (a & b); println (a ^ b); println ( ~ b); a = 16; b = 12; println (a << b); } }

Output:

0

15

15

10

4

64

4

Recommended Articles

We hope that this EDUCBA information on “Scala Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Learn 5 Major Types Of Scala Operators

Update the detailed information about Learn 5 Major Types Of Scala Operators on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!