Scala Operators

Operators in Scala are symbols that perform operations on values. Scala treats operators as method calls, which means every operator you use is actually a method defined on an object. This design makes Scala's operator system both consistent and extensible.

Arithmetic Operators

val a = 20
val b = 6

println(a + b)   // 26  — addition
println(a - b)   // 14  — subtraction
println(a * b)   // 120 — multiplication
println(a / b)   // 3   — integer division (truncates)
println(a % b)   // 2   — modulo (remainder)

Integer division always truncates toward zero. To get a decimal result, use Double values:

println(20.0 / 6)    // 3.3333...
println(20 / 6.0)    // 3.3333...

Operators Are Methods

In Scala, a + b is syntactic sugar for a.+(b). Every operator is a method call on the left-hand object:

val x = 10
val y = 3

x + y       // same as x.+(y)   → 13
x - y       // same as x.-(y)   → 7
x * y       // same as x.*(y)   → 30
x / y       // same as x./(y)   → 3

Comparison Operators

val p = 15
val q = 20

println(p == q)   // false — equal
println(p != q)   // true  — not equal
println(p < q)    // true  — less than
println(p > q)    // false — greater than
println(p <= q)   // true  — less than or equal
println(p >= q)   // false — greater than or equal

Comparison operators always return a Boolean. In Scala, == checks value equality (not reference identity like Java). Use eq to compare object references.

Logical Operators

val isRaining = true
val hasUmbrella = false

println(isRaining && hasUmbrella)   // false — AND (both must be true)
println(isRaining || hasUmbrella)   // true  — OR  (at least one true)
println(!isRaining)                 // false — NOT (inverts)

Truth Table:
A       B       A && B    A || B    !A
─────   ─────   ──────    ──────    ──
true    true    true      true      false
true    false   false     true      false
false   true    false     true      true
false   false   false     false     true

Short-circuit evaluation: In A && B, if A is false, Scala skips evaluating B entirely. In A || B, if A is true, Scala skips B. This prevents errors and improves performance.

Bitwise Operators

val m = 12   // binary: 1100
val n = 10   // binary: 1010

println(m & n)    // 8  → 1000 — AND each bit
println(m | n)    // 14 → 1110 — OR  each bit
println(m ^ n)    // 6  → 0110 — XOR each bit
println(~m)       // -13       — flip all bits
println(m << 1)   // 24 — left shift (multiply by 2)
println(m >> 1)   // 6  — right shift (divide by 2)

  1100  (12)      1100  (12)
& 1010  (10)    | 1010  (10)
──────          ──────
  1000  (8)       1110  (14)

Assignment Operators

var score = 100

score += 10    // score = score + 10  → 110
score -= 5     // score = score - 5   → 105
score *= 2     // score = score * 2   → 210
score /= 3     // score = score / 3   → 70
score %= 8     // score = score % 8   → 6

println(score)   // 6

String Operators

val first = "Hello"
val second = "World"

println(first + " " + second)   // Hello World  — concatenation
println(first * 3)               // HelloHelloHello — repeat
println(first == "Hello")        // true
println(first > "Apple")         // true (lexicographic)

Operator Precedence

Scala follows standard mathematical precedence. Multiplication runs before addition, just as in algebra:

println(2 + 3 * 4)      // 14  (not 20) — * before +
println((2 + 3) * 4)    // 20  — parentheses override precedence
println(10 - 4 - 2)     // 4   — left to right
println(2 + 3 > 4)      // true — arithmetic before comparison

Highest → Lowest precedence:
  *  /  %
  +  -
  :
  =  !
  <  >
  &
  ^
  |
  (all letters)
  Assignment: += -= *= /= %=

Custom Operators

Because operators are methods, you can define your own by using operator symbols as method names:

case class Vector2D(x: Double, y: Double):
  def +(other: Vector2D): Vector2D =
    Vector2D(x + other.x, y + other.y)
  def *(scalar: Double): Vector2D =
    Vector2D(x * scalar, y * scalar)
  override def toString: String = s"($x, $y)"

val v1 = Vector2D(1.0, 2.0)
val v2 = Vector2D(3.0, 4.0)

println(v1 + v2)    // (4.0, 6.0)
println(v1 * 3.0)   // (3.0, 6.0)

The == vs eq Distinction

val s1 = "Scala"
val s2 = "Scala"
val s3 = new String("Scala")

println(s1 == s2)    // true  — same value
println(s1 == s3)    // true  — same value
println(s1 eq s2)    // true  — same object (JVM string interning)
println(s1 eq s3)    // false — different objects in memory

Always use == for value comparisons in Scala. Reserve eq for rare cases where reference identity matters.

Leave a Comment

Your email address will not be published. Required fields are marked *