Skip to content

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:

Perform standard mathematical calculations. Applicable primarily to numeric types (Int, Double, Float, Long, etc.).

Assume val a = 10, val b = 20:

OperatorDescriptionExample
+Additiona + b results in 30
-Subtractionb - a results in 10
*Multiplicationa * b results in 200
/Divisionb / a results in 2 (integer division)
%Modulus (Remainder)b % 3 results in 2
- (unary)Negation-a results in -10

Compare two values, resulting in a Boolean (true or false).

Assume val a = 10, val b = 20:

OperatorDescriptionExample
==Equal toa == 10 is true
!=Not equal toa != b is true
>Greater thanb > a is true
<Less thana < b is true
>=Greater than or equal toa >= 10 is true
<=Less than or equal tob <= 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).

Combine boolean expressions.

Assume val x = true, val y = false:

OperatorDescriptionExample
&&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).

Operate on the individual bits of integer types (Int, Long, Byte, Short).

Assume val a = 60 (0011 1100), val b = 13 (0000 1101):

OperatorDescriptionExample
&Bitwise ANDa & b results in 12 (0000 1100)
|Bitwise ORa | b results in 61 (0011 1101)
^Bitwise XORa ^ b results in 49 (0011 0001)
~Bitwise Complement (unary)~a results in -61 (1100 0011 in 2’s complement)
<<Left Shifta << 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

Used to assign values to mutable variables (var). Includes compound assignment operators.

Assume var x = 5, val y = 3:

OperatorDescriptionExample
=Simple Assignmentx = 10 (x becomes 10)
+=Add and Assignx += y (equivalent to x = x + y, x becomes 8)
-=Subtract and Assignx -= y (equivalent to x = x - y)
*=Multiply and Assignx *= y (equivalent to x = x * y)
/=Divide and Assignx /= y (equivalent to x = x / y)
%=Modulus and Assignx %= y (equivalent to x = x % y)
&=, |=, ^=Bitwise and Assignx &= y (equivalent to x = x & y)
<<=, >>=, >>>=Shift and Assignx <<= 1 (equivalent to x = x << 1)

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):”
  1. Postfix operators (e.g., method calls (), selection .)
  2. Unary operators (+, -, !, ~)
  3. Multiplicative (*, /, %)
  4. Additive (+, -)
  5. Shift (<<, >>, >>>)
  6. Relational (<, >, <=, >=)
  7. Equality (==, !=, eq, ne)
  8. Bitwise AND (&)
  9. Bitwise XOR (^)
  10. Bitwise OR (|)
  11. Logical AND (&&)
  12. Logical OR (||)
  13. 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.