Scala Operators
Scala Operators
Section titled “Scala Operators”Operators in Scala are typically just methods with symbolic names. Scala allows methods to be used in infix notation when they take one parameter, making expressions like a + b look like standard operator usage, while actually being equivalent to a.+(b).
Scala supports the familiar set of operators found in languages like Java or C++, categorized as follows:
Arithmetic Operators:
Section titled “Arithmetic Operators:”Perform standard mathematical calculations. Applicable primarily to numeric types (Int, Double, Float, Long, etc.).
Assume val a = 10, val b = 20:
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b results in 30 |
- | Subtraction | b - a results in 10 |
* | Multiplication | a * b results in 200 |
/ | Division | b / a results in 2 (integer division) |
% | Modulus (Remainder) | b % 3 results in 2 |
- (unary) | Negation | -a results in -10 |
Relational Operators:
Section titled “Relational Operators:”Compare two values, resulting in a Boolean (true or false).
Assume val a = 10, val b = 20:
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == 10 is true |
!= | Not equal to | a != b is true |
> | Greater than | b > a is true |
< | Less than | a < b is true |
>= | Greater than or equal to | a >= 10 is true |
<= | Less than or equal to | b <= 20 is true |
Note: In Scala, == performs a sensible comparison (value comparison for value types, equals method for reference types), unlike Java where == is reference equality for objects. For reference equality in Scala, use eq (e.g., ref1 eq ref2).
Logical Operators:
Section titled “Logical Operators:”Combine boolean expressions.
Assume val x = true, val y = false:
| Operator | Description | Example |
|---|---|---|
&& | Logical AND (short-circuiting) | x && y is false |
|| | Logical OR (short-circuiting) | x || y is true |
! | Logical NOT (unary) | !y is true |
Short-circuiting means the second operand is only evaluated if necessary (e.g., in false && Z, Z is never evaluated).
Bitwise Operators:
Section titled “Bitwise Operators:”Operate on the individual bits of integer types (Int, Long, Byte, Short).
Assume val a = 60 (0011 1100), val b = 13 (0000 1101):
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | a & b results in 12 (0000 1100) |
| | Bitwise OR | a | b results in 61 (0011 1101) |
^ | Bitwise XOR | a ^ b results in 49 (0011 0001) |
~ | Bitwise Complement (unary) | ~a results in -61 (1100 0011 in 2’s complement) |
<< | Left Shift | a << 2 results in 240 (1111 0000) |
>> | Arithmetic Right Shift (preserves sign) | a >> 2 results in 15 (0000 1111) |
>>> | Logical Right Shift (fills with zero) | a >>> 2 results in 15 (0000 1111); (~a) >>> 2 would differ from (~a) >> 2 |
Assignment Operators:
Section titled “Assignment Operators:”Used to assign values to mutable variables (var). Includes compound assignment operators.
Assume var x = 5, val y = 3:
| Operator | Description | Example |
|---|---|---|
= | Simple Assignment | x = 10 (x becomes 10) |
+= | Add and Assign | x += y (equivalent to x = x + y, x becomes 8) |
-= | Subtract and Assign | x -= y (equivalent to x = x - y) |
*= | Multiply and Assign | x *= y (equivalent to x = x * y) |
/= | Divide and Assign | x /= y (equivalent to x = x / y) |
%= | Modulus and Assign | x %= y (equivalent to x = x % y) |
&=, |=, ^= | Bitwise and Assign | x &= y (equivalent to x = x & y) |
<<=, >>=, >>>= | Shift and Assign | x <<= 1 (equivalent to x = x << 1) |
Operator Precedence:
Section titled “Operator Precedence:”Determines the order in which operators are evaluated in an expression. Scala’s precedence rules are generally similar to Java/C++.
Simplified Precedence Order (Highest to Lowest):
Section titled “Simplified Precedence Order (Highest to Lowest):”- Postfix operators (e.g., method calls
(), selection.) - Unary operators (
+,-,!,~) - Multiplicative (
*,/,%) - Additive (
+,-) - Shift (
<<,>>,>>>) - Relational (
<,>,<=,>=) - Equality (
==,!=,eq,ne) - Bitwise AND (
&) - Bitwise XOR (
^) - Bitwise OR (
|) - Logical AND (
&&) - Logical OR (
||) - Assignment (
=,+=, etc.)
For operators not listed (like custom symbolic methods), precedence is determined by the first character of the operator name, with characters like *, /, % having higher precedence than +, -, which have higher precedence than comparison operators, etc. Assignment operators have the lowest precedence. Parentheses () can always be used to explicitly control the order of evaluation.
Example: val result = 2 + 3 * 4 results in 14 because * has higher precedence than +. val result2 = (2 + 3) * 4 results in 20.