Tutorials
scala

Operators in Scala

Learn about the different operators used in the Scala programming language.

Note: This tutorial is a continuation of the Scala Tutorial Series. If you have not read Beginners Guide to Scala and IF ELSE in Scala, then check them out.

Introduction

An operator is a symbol or a function that signifies an operation to be performed with one or more operand. There are many different types of operators, while an operand can be thought of as a variable. Operators are the base of all the programming language. Operators allow us to perform various kinds of operations on operands. Operators inform the compiler to perform operators such as mathematical (arithmetic), logical, assignment, etc. and finally output the result.

There are different types of operators that are used in many programming languages as well as in Scala, which are as follows:

  • Relational Operator
  • Arithmetic Operator
  • Assignment Operator
  • Logical Operator
  • Bitwise Operator

You will be learning about the above operators both theoretically and programmatically.

Relational Operators

Relational operators are mainly used for performing a comparison between two or more variables. If you have read the IF ELSE in Scala tutorial then you would have observed, you had leveraged the power of relational operators in the IF ELSE conditions.

These relational operators return a boolean value, i.e., True or False.

Following are the relational operator symbols that you use to apply them on the variables (Source: Operators in Scala):

  • Equal To(==): It checks whether the two given variables or operands are equal or not. If they are, then it returns true else it returns false.

  • Not Equal To(!=): It is opposite of the equal to operator, or you can say a boolean complement of equal to operator. It checks whether the two given operands are equal or not. If not, then it returns true else it returns false.

  • Greater Than(>): It checks whether the first operand is higher than the second operand. If it is, then it returns true else it returns false.

  • Less than(<): It is opposite of the greater than operator since it checks whether the first operand is lesser than the second operand. If so, it returns true else it returns false.

  • Greater Than Equal To(>=): This operator checks whether the first operand is greater than or equal to the second operand. If it is, it returns true; otherwise, it returns false.

  • Less Than Equal To(<=): An opposite of the greater than equal to operator. It checks whether the first operand is lesser than or equal to the second operand. If so, it returns true; otherwise, it returns false.

Let's now understand relational operators with the help of a code.


To run the above code, all you need to do is type scala relation_op.scala and you should see an output as shown below.


Next, you'll learn about the logical operators.

Logical Operators

They are mostly used in conditional statements like an IF ELSE statement or even in a while loop. It amalgamates two or more conditions or also complements the output of the original condition, which is considered. (Source: Operators in Scala)

  • Logical AND: It returns true when both the conditions are sufficed. Otherwise, it returns false. Using “and” acts as a proxy for the && operator.

  • Logical OR: It returns true when one (or both) of the conditions are satisfied. Otherwise, it returns false. Using “or” acts as a proxy for || operator.

  • Logical NOT: It returns true if the condition being considered is not satisfied. Otherwise, it returns false. Using “not” acts as an alternative for ! operator.

Arithmetic (Mathematical) Operators

As the name suggests, the arithmetic operators are used to perform mathematical operations between operands.

Following are the arithmetic operators that are mostly used in Scala (Source: Operators in Scala):

  • Addition: Adds $(+)$ two variables/operands.

  • Subtraction: Subtracts $(-)$ two operands.

  • Multiplication: Multiplies $(\times)$ two operands.

  • Division: Divides $(/)$ the first operand with the second operand.

  • Exponent: It returns the exponential(power) of the operands. Basically, when one operand is raised to the power of the second operand $(x^y)$ or $(x**y)$.

  • Modulus: When the first operand is divided by the second operand, the leftover or the remainder is known as the modulus (%).

Let's quickly write a small piece of code and analyze the results.

In this example, you will be making use of lists instead of defining just variables. Unlike a variable, a List can hold n number of values with various datatypes, they represent linked list in scala and are immutable. You will be performing the arithmetic operations on the two lists instead of two variables.

Assignment Operators

To assign a value to a variable, assignment operators are used. To accomplish this, the right side operand is considered as a value, while the left side operand is usually a variable to which the value is assigned. The value can have different data-types like int, float, etc. The data-type of both variable and value should be same otherwise the compiler raises an error. (Source: Operators in Scala)

  • Simple Assignment: This operator is used to assign a value to the variable $(=)$.

  • Add AND Assignment: It is used for adding left operand with right operand finally, assigning it to the variable on the left side $(+=)$.

  • Subtract AND Assignment: Similar to the Add AND assignment. This is used for subtracting left operand with right operand finally, assigning it to a variable on the left side $(-=) $.

  • Multiply AND Assignment: It is used for multiplying the left operand with right operand and then assigning it to the variable on the left side $(\times =)$.

  • Divide AND Assignment: It is used for dividing left operand with right operand and finally assigning the value to the variable on the left side $(/=)$.

Similarly, there are many more such assignment operators. Finally, let's write a code to understand how they work programmatically.

Conclusion

Congratulations on completing this tutorial on Scala.

If you have read the previous two tutorials, you have now covered three different topics in the Scala. A useful exercise would be to the skills gained from all three and write one Scala program from scratch. This will not only boost your confidence but will also make your life easier while following along with much more advanced topics in Scala.

References:

Please feel free to ask any questions related to this tutorial in the comments section below.

Want to leave a comment?