Scala

Scala Operators

In general, operators are symbols that perform specific operations on specific values. These cues are known as operands. The operators include arithmetic, logical, assignment, bitwise, relation operators, and some more. As a part of this Scala article, we will discuss Scala operators and get a clear understanding of the aspects of the Scala programming language with their syntax and example codes.

What are Operators in Scala?

Operator is associated with the unique code and syntax that executes to perform a task. The operators are used between the operands. These operands can be one or more based on the operation.

Types and Examples of Scala Operators in Ubuntu 20.04?

There is a huge range of operations in the Scala programming language. We will discuss common operators with their subtypes along with the example codes.

Example # 1: Using Arithmetic Operator in Scala

As the name suggests, they are used to perform arithmetic or you can say mathematical operations between two values. Following are the arithmetic operators widely used in Scala.

Addition: The sign of the addition operator is “+” which adds two variables or operands.

Subtraction: Indicated with the sign “ -”. The operator used between the two values for subtracting.

Multiplication: The operator uses the “*” sign for multiplying two operands or more.

Division: The operator Division is denoted with the sign “/”divides the first operand by the second one.

Exponent: It specifies with the sign “**” known as the power. It returns the exponential value of the Operand.

Let’s execute the example programmatically. Here below, we have initialized the variables with different values and in the print statement, we have called all the arithmetic operations.

The output values of all the arithmetic operations are shown in the below image.

Example # 2: Using Relational Operator in Scala

The relational operator defines the comparison between the operand which is also known as the Comparison operator. The operator is used on conditional statements and returns the Boolean value true or false. Following are subtypes of Relational operators.

Equal to (=): The equal to operator compares the two operands whether they are equal or not and return a Boolean value true else false.

Not equal to (!=): The operator is used for comparison whether two operands are not equal to one given condition.

Greater than (>): It is used for checking the greater values of the given operand. If the given value has to be greater than returns true otherwise false.

Less than (<): The less-than operator is used to check the smaller value from the operand given and return true if the operand is smaller than the second operand otherwise we have a false value.

Lesser than equal to(<=): It is used to verify that a given operand should be smaller than and equal to the second operand. In the end, we will have true or false conditions.

We have an example code of relational operators below. As you can see, we have used all the operators discussed above in Relational operators. Through the print function, we can have the output results.

As displaying the output result value by using Relational operators.

Example # 3: Using Logical Operator in Scala

The logical operator is utilized to associate the logical output as true value or false value. It also reverses the logical output of a conditional operand.

Logical AND (&&): The AND operator between two operands returns a true value if both the operands are non-zero values as 1 else returns false.

Logical OR (||): If one of the given operands is non-zero in the logical OR Operator, we will have true as a return value otherwise false is the return value.

Logical NOT (!): It will give the opposite value of operand given i.e. if true then false vice versa.

Now, we have an example code. We have initialized a Boolean value in a variable. The print function will return the output of the variable after using Logical operators.

The output is shown below on the terminal screen.

Example # 4: Using Bitwise Operator in Scala

The Bitwise operator performs the individual bit level operator on operands such that it works bit by bit on every operand. Below, we have bitwise operators.

Bitwise AND(&): The operator takes two operands and performs the “AND” operation on every bit value.

Bitwise OR(|): It checks bitwise operation on every given operand whether the bit values are zero or not. If both bits are zero then it will return 0 else it will return 1 as output.

Bitwise XOR(^): If both bits are the same then it will return 0 else return 1. It will check the operand bitwise.

Bitwise -NOT(~): The value we will have after using Bitwise -NOT is a complement value from the operand. It changes from 1 to 0 and vice versa.

Bitwise Left shift(<<): In this operator, the operands shifted to the left side by a number if bit given in the right side of the operand

Bitwise Right shift(>>): The operator shifted the bit of the left operand to the right side by several bits specified by the right operand

Bitwise Right shift zero-fill(>>>): It shifts the left operand to the right by right operand with a specific number of bits and the zero is filled up with the shifted values.

Let’s have an example code of the Bitwise operator. We have used all the bitwise operators mentioned above.

The output value we get from different assignment operators is on the screen below.

Conclusion

In the article, we have a deep discussion on Scala Operators. We have learned four major operators that include Arithmetic operator, Logical Operator, Relational operator, and Bitwise operator. Also, we have a great understanding of operator subtypes with their running example codes. This guide will assist in the Scala scripts.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.