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:
Each of these has the same importance and use in Scala as in any other language.
These are the six relational operators:
Equal to (==
): compares two operands. If they are the same, the function returns true. Otherwise, it returns false.
Not equal to (!=
): compares two operands and returns true if they are not equal.
Greater than (>
): finds and returns the max value from operands.
Less than (<
): finds and returns the min value from operands.
Greater than equal to (>=
): returns true if the operand on the left is equal to and/or greater than the operand on the left.
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.
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)) }
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:
The assignment operator (=
): assigns a value to a variable.
Add and assignment (+=
): takes two operands, adds them, and assigns the value to the operand on the left.
Subtract and assignment (-=
): takes two operands, subtracts them, and assigns the value to the operand on the left.
Multiply and assignment (*=
): takes two operands, multiplies them, and assigns the value to the operand on the left.
Divide and assignment (/=
): takes two operands, divides them, and assigns the value to the operand on the left.
Modulus and assignment (%=
): takes two operands, divides them, and assigns the remainder to the operand on the left.
Right shift and assignment (>>=
): performs binary right shift on the left operand with the right operand, and assigns the value to the left operand.
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:
Bitwise AND and assignment (&=
): performs binary AND on the left operand with the right operand, and assigns the value to the left operand.
Bitwise inclusive OR and assignment (|=
): performs binary OR on the left operand with the right operand, and assigns the value to the left operand.
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.
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) }
We use these operators depending on what type of situation is in command. There are three basic logical operators:
And (&&
): returns true
if both operands have true
value.
OR (||
): returns true
if either of the operands is true
.
NOT (!
): returns the opposite of boolean operand.
We apply these operators to expressions and return a boolean value.
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)) }
These are basic mathematic operators that are used almost everywhere:
Addition (+
): adds two numbers together.
Subtraction (-
): returns the subtraction of two numbers.
Multiplication (*
): multiplies two numbers.
Division (/
): divides and returns the result of two numbers.
Modulus (%
): returns the remainder after division.
Note: In Scala, the exponent of a value cannot be taken with (
**
) operator, so we must use thescala.math.pow(x,y)
library to take an exponent.
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))) }
Unlike other operators, Bitwise operators work on a bit by bit level.
Bitwise AND (&
): performs AND operation on every bit of both operands. Returns 1 only if both bits are 1.
Bitwise OR (|
): performs OR operation on every bit of both operands. Returns 1 if any bit is 1.
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
CONTRIBUTOR
View all Courses