Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What are operators in Scala?

Dian Us Suqlain

An operator is a symbol or function that performs some operation between two or more values. There are multiple types of operators in programming languages, such as:

  • relational operators
  • assignment operators
  • logical operators
  • arithmetic operators
  • bitwise operators

Each of these has the same importance and use in Scala as in any other language.

Relational operators

These are the six relational operators:

  1. Equal to (==): compares two operands. If they are the same, the function returns true. Otherwise, it returns false.

  2. Not equal to (!=): compares two operands and returns true if they are not equal.

  3. Greater than (>): finds and returns the max value from operands.

  4. Less than (<): finds and returns the min value from operands.

  5. Greater than equal to (>=): returns true if the operand on the left is equal to and/or greater than the operand on the left.

  6. Less than equal to (<=): returns true if the operand on the left is equal to and/or smaller than the operand on the left.

We often use relational operators in the conditional If-else statements in code because they return either of the values, i.e., true or false.

Note: An operand is anything that has a value. Suppose 3 + 4: here (+) is an operator and digits on either side of the operator are operands.

Example 01 - Relational operators

object Main extends App {
    var a = 69
    var b = 40

    println("1. Equal to (==):              Is a == b? " + (a == b))
    println("2. Not equal to (!=):          Is a != b? " + (a != b))
    println("3. Greater than (>):           Is a > b?  " + (a > b))
    println("4. Less than (<):              Is a < b?  " + (a < b))
    println("5. Greater than equal to (>=): Is a >= b? " + (a >= b))
    println("6. Less than equal to (<=):    Is a <= b? " + (a <= b))
}
Relational Operators - Scala

Assignment operators

In Scala, there are 11 assignment operators. We use these operators to assign values to a variable.

We place the variables to which the values are assigned on the left side of the operator, while their values or expressions are written on the right side.

The operators are as follows:

  1. The assignment operator (=): assigns a value to a variable.

  2. Add and assignment (+=): takes two operands, adds them, and assigns the value to the operand on the left.

  3. Subtract and assignment (-=): takes two operands, subtracts them, and assigns the value to the operand on the left.

  4. Multiply and assignment (*=): takes two operands, multiplies them, and assigns the value to the operand on the left.

  5. Divide and assignment (/=): takes two operands, divides them, and assigns the value to the operand on the left.

  6. Modulus and assignment (%=): takes two operands, divides them, and assigns the remainder to the operand on the left.

  7. Right shift and assignment (>>=): performs binary right shift on the left operand with the right operand, and assigns the value to the left operand.

  8. Left shift and assignment (<<=): performs binary left shift on the left operand with the right operand, and assigns the value to the left operand.

Bitwise assignment operators:

  1. Bitwise AND and assignment (&=): performs binary AND on the left operand with the right operand, and assigns the value to the left operand.

  2. Bitwise inclusive OR and assignment (|=): performs binary OR on the left operand with the right operand, and assigns the value to the left operand.

  3. Bitwise exclusive OR and assignment (^=): performs binary XOR on the left operand with the right operand, and assigns the value to the left operand.

Note: Data types on both sides of the assignment operator should be the same, otherwise, the compiler would throw an error message.

Example 02 - Assignment operators

Let’s observe the working of these operators with the help of a program.

object Main extends App {
    var x = 69
    var y = 30
    var z = x + y

    z += x
    println("1. Add and assignment operator (+=):          " + z)
    
    z -= x
    println("2. Subtract and assignment operator (+=):     " + z)
    
    z *= x
    println("3. Multiply and assignment operator (*=):     " + z)
    
    z /= x
    println("4. Divide and assignment operator (/=):       " + z)
    
    z %= x
    println("5. Modulus and assignment operator (%=):      " + z)
    
    z >>= 2
    println("6. Right shift and assignment operator (>>=): " + z)

    z <<= 2
    println("7. Left shift and assignment operator (<<=):  " + z)

    z &= x
    println("8. Bitwise and assignment operator (&=):      " + z)

    z |= x
    println("9. Inclusive OR and assignment operator (|=): " + z)

    z ^= x
    println("10. Exclusive OR and assignment operator (^=): " + z)
}

Logical operators

We use these operators depending on what type of situation is in command. There are three basic logical operators:

  1. And (&&): returns true if both operands have true value.

  2. OR (||): returns true if either of the operands is true.

  3. NOT (!): returns the opposite of boolean operand.

We apply these operators to expressions and return a boolean value.

Example 03 - Logical operators

object Main extends App {
    var x = true
    var y = false

    println("1. Logical AND operator (&&): " + (x && y))
    println("2. Logical OR operator (||):  " + (x || y))
    println("3. Logical NOT operator (!):  " + (!x))

}

Arithmetic operators

These are basic mathematic operators that are used almost everywhere:

  1. Addition (+): adds two numbers together.

  2. Subtraction (-): returns the subtraction of two numbers.

  3. Multiplication (*): multiplies two numbers.

  4. Division (/): divides and returns the result of two numbers.

  5. Modulus (%): returns the remainder after division.

Note: In Scala, the exponent of a value cannot be taken with (**) operator, so we must use the scala.math.pow(x,y) library to take an exponent.

Example 04 - Arithmetic operators

object Main extends App {
    var x = 6
    var y = 3

    println("1. Addition operator (+):       " + (x + y))
    println("2. Subtraction operator (-):    " + (x - y))
    println("3. Division operator (/):       " + (x / y))
    println("4. Multiplcation  operator (*): " + (x * y))
    println("5. Modulus operator (%):        " + (x % y))
    println("6. Exponent operator (**):      " + (scala.math.pow(x,y)))

}

Bitwise operators

Unlike other operators, Bitwise operators work on a bit by bit level.

  1. Bitwise AND (&): performs AND operation on every bit of both operands. Returns 1 only if both bits are 1.

  2. Bitwise OR (|): performs OR operation on every bit of both operands. Returns 1 if any bit is 1.

  3. Bitwise XOR (^): performs XOR operation on every bit of both operands. Returns 1 if both bits are the same.

object Main extends App {
    var x = 6
    var y = 3
    var z = 0

    z = x & y
    println("1. Bitwise AND (&): " + z)
    
    z = x | y
    println("2. Bitwise OR (|):  " + z)
    
    z = x ^ y
    println("3. Bitwise XOR (^): " + z)
    
}

RELATED TAGS

scala

CONTRIBUTOR

Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring