Scala Operators

Learn via video courses
Topics Covered

Overview

Scala operators are special symbols that represent various operations on values, such as arithmetic operations, logical operations, comparisons, etc. Scala provides a rich set of built-in operators, and we can also define our own custom operators using symbolic names. Scala's rich operator system enhances code readability and conciseness, bridging mathematical concepts with practical programming tasks.

Introduction

Scala operators are the fundamental building blocks of expressive and efficient programming, enabling us to perform a diverse range of operations on data. Rooted in both object-oriented and functional programming paradigms, Scala offers a comprehensive set of operators that facilitate arithmetic computations, logical evaluations, comparisons, and more.

Scala operators provide a bridge between mathematical abstractions and real-world programming challenges, allowing us to succinctly articulate complex actions. From traditional arithmetic symbols like +, -, *, and /, to advanced logical constructs such as &&, ||, and custom user-defined operators, Scala empowers programmers to craft elegant and concise code.

What are the Operators in Scala?

Scala operators are special symbols or tokens that perform various operations on values, variables, or expressions. Scala operations can include arithmetic calculations, logical evaluations, comparisons, and more, that helps to manipulate data and express complex computations concisely.

Scala, has rules governing the order in which operators are evaluated. Scala operators precedence determines which operators are evaluated first, while associativity determines the order of evaluation for operators with the same precedence. It's essential to understand these rules to write correct and predictable code.

operators-in-scala1

Different Types of Operators in Scala

Scala operators can be broadly categorized into several types based on their functionality. Let us understand them in detail.

Arithmetic Operators

Arithmetic operators in Scala are symbols used to perform basic mathematical calculations on numeric values. These operators allow us to add, subtract, multiply, divide, and find the remainder between numbers. Let's explore different types of arithmetic operator:

  • Addition +: The addition operator + is used to add two numeric values together.
  • Subtraction -: The subtraction operator - is used to subtract one numeric value from another.
  • Multiplication *: The multiplication operator * is used to multiply two numeric values.
  • Division /: The division operator / is used to divide one numeric value by another. If the operands are integers, the result will be truncated to an integer. To obtain a floating-point result, at least one of the operands should be a floating-point number.
  • Modulo %: The modulo operator % calculates the remainder of the division between two numeric values. It is often used to determine whether a number is even or odd or to cycle through a range of values.
  • Unary Negation - (Negate): The unary negation operator - is used to negate a numeric value, changing its sign.
  • Operator Precedence: Operators in Scala follow a specific precedence and associativity. For instance, multiplication and division have higher precedence than addition and subtraction. If we want to alter the order of evaluation, we can use parentheses.

Relational Operators

Relational operators in Scala are used to compare values and determine their relationship in terms of order or equality. These operators yield a Boolean value (either TRUE or FALSE) depending on the outcome of the comparison. Here are the relational operators in Scala:

  • Equal to ==: The == equality operator evaluates two values and delivers TRUE if they are equivalent, while returning FALSE if they differ.
  • Not equal to !=: The != inequality operator verifies whether two values are dissimilar. It produces TRUE when the values are unequal, and FALSE when they match.
  • Less than <: The less than operator < compares whether the value on the left is less than the value on the right.
  • Greater than >: The greater than operator > checks if the value on the left is greater than the value on the right.
  • Less than or equal to <=: The less than or equal to operator <= compares whether the value on the left is less than or equal to the value on the right.
  • Greater than or equal to >=: The greater than or equal to operator >= checks if the value on the left is greater than or equal to the value on the right.

operators-in-scala2

Logical Operators

Logical operators in Scala are used to perform logical operations on boolean values or expressions. These operators allow us to combine, negate, or evaluate the truth value of conditions. There are three main logical operators in Scala:

  • Logical AND &&: The && logical AND operator yields TRUE only when both operands are true; otherwise, it results in FALSE.
  • Logical OR ||:

The || logical OR operator yields a TRUE result if at least a single operand evaluates to true; otherwise, it produces a FALSE outcome.

  • Logical NOT !: The logical NOT operator ! is used to negate a boolean expression. It returns TRUE if the operand is false, and FALSE if the operand is true.

Bitwise Operators

Bitwise operators in Scala are used to manipulate individual bits of integer values. These operators perform bitwise operations at the binary level, allowing us to modify and analyze the binary representation of integers.

  • Bitwise AND &: The & bitwise AND operator executes a logical AND operation on corresponding bits of two integers, setting a bit to 1 solely if both corresponding bits in the operands are 1.
  • Bitwise OR |: The | bitwise OR operator conducts a logical OR operation on corresponding bits of two integers, setting a bit to 1 if at least one corresponding bit in the operands is 1.
  • Bitwise XOR ^: The bitwise XOR operator ^ performs a logical XOR (exclusive OR) operation between the corresponding bits of two integers. It sets a bit to 1 if exactly one corresponding bit in the operands is 1.
  • Bitwise NOT ~: The bitwise NOT operator ~ (tilde) inverts the bits of an integer, changing 1s to 0s and 0s to 1s.
  • Left Shift << and Right Shift >>: The left shift operator << shifts the bits of an integer to the left by a specified number of positions, filling the empty positions with zeros. The right shift operator >> shifts the bits to the right.
  • Unsigned Right Shift >>>: The unsigned right shift operator >>> shifts the bits to the right, filling the empty positions with zeros, regardless of the sign bit.

Assignment Operators

Assignment operators in Scala are used to assign values to variables while performing a specific operation in a single step. These operators combine the task of assignment with another operation like addition, subtraction, multiplication, division, etc. Here are the assignment operators in Scala:

  • Simple Assignment =: The basic assignment operator = assigns the value from the right-hand side to the variable located on the left-hand side.
  • Addition Assignment +=: The addition assignment operator += adds the value on the right to the existing value of the variable on the left and then assigns the result back to the variable.
  • Subtraction Assignment -=: The subtraction assignment operator -= subtracts the value on the right from the existing value of the variable on the left and assigns the result back to the variable.
  • Multiplication Assignment *=: The multiplication assignment operator *= multiplies the existing value of the variable on the left by the value on the right and assigns the result back to the variable.
  • Division Assignment /=: The division assignment operator /= divides the existing value of the variable on the left by the value on the right and assigns the result back to the variable.
  • Modulo Assignment %=: The modulo assignment operator %= calculates the remainder of dividing the existing value of the variable on the left by the value on the right and assigns the result back to the variable.

Conclusion

  • Scala operators are symbols that perform actions like arithmetic computations (+, -), logical evaluations (&&, ||), comparisons (==, <), and more on values, enabling concise expression of operations. Custom operators can also be created for specific tasks.
  • Arithmetic operators in Scala perform mathematical calculations on numeric values, including addition, subtraction, multiplication, division, and modulo.
  • Relational operators in Scala compare values to determine their relationship in terms of order or equality, returning boolean results.
  • Logical operators in Scala combine boolean values or expressions to evaluate logical conditions, producing boolean outcomes.
  • Bitwise operators in Scala manipulate individual bits of integers through binary operations, often used for low-level tasks and optimizations.
  • Assignment operators in Scala combine value assignment with specific operations, offering concise ways to modify variable values in a single step.