Scala Operators
Scala has the usual arithmetic, comparison and logical operators — but with a twist: operators are just methods. 1 + 2 is shorthand for 1.+(2). This makes the language very uniform, and it lets your own classes define operators too.
Arithmetic operators
| Operator | Meaning | 7 op 3 |
|---|---|---|
+ |
addition | 10 |
- |
subtraction | 4 |
* |
multiplication | 21 |
/ |
division | 2 (integer division) |
% |
remainder | 1 |
@main def run(): Unit =
val a = 7
val b = 3
println(a + b)
println(a - b)
println(a * b)
println(a / b)
println(a % b)
println(a.toDouble / b)
println(-a % b)
println(Math.floorMod(-a, b))10
4
21
2
1
2.3333333333333335
-1
2
Note: Scala has no
++or--operators. Writecount += 1instead.
Assignment operators
For a var, every arithmetic operator has a compound form: +=, -=, *=, /=, %=.
@main def run(): Unit =
var score = 10
score += 5
score *= 2
score -= 4
score /= 2
println(score)13
Comparison operators
| Operator | Meaning |
|---|---|
== |
equal (compares values) |
!= |
not equal |
< <= > >= |
ordering |
eq / ne |
same object reference (rarely needed) |
@main def run(): Unit =
val x = 5
println(x == 5)
println(x != 5)
println(x >= 3 && x < 10)
val s1 = new String("hi")
val s2 = new String("hi")
println(s1 == s2)
println(s1 eq s2)true
false
true
true
false
== compares contents; eq checks whether both names point to the very same object.
Logical operators
| Operator | Meaning |
|---|---|
&& |
and (short-circuits) |
|| |
or (short-circuits) |
! |
not |
Short-circuiting means the right side is evaluated only when needed:
def check(label: String, result: Boolean): Boolean =
println(s"checking $label")
result
@main def run(): Unit =
println(check("A", false) && check("B", true))
println(check("C", true) || check("D", true))
println(!true)checking A
false
checking C
true
false
Operators are methods
Because operators are methods, you can call methods using infix syntax and define operators on your own types.
case class Money(cents: Long):
def +(other: Money): Money = Money(cents + other.cents)
def *(factor: Int): Money = Money(cents * factor)
override def toString = f"€${cents / 100.0}%.2f"
@main def run(): Unit =
val coffee = Money(250)
val cake = Money(375)
println(coffee + cake)
println(coffee * 3)
println(1.+(2))
println(List(1, 2, 3).contains(2))
println(10 max 4)€6.25
€7.50
3
true
10
10 max 4 calls the method max in infix style. For alphanumeric methods, Scala 3 prefers dot syntax (10.max(4)) unless the method is marked infix, but both still compile for common cases like this.
Bitwise operators
| Operator | Meaning | 6 op 3 |
|---|---|---|
& |
AND | 2 |
| |
OR | 7 |
^ |
XOR | 5 |
~ |
NOT (unary) | ~6 is -7 |
<< / >> |
shift left / right | 6 << 1 is 12 |
Precedence
Operators that start with *, / or % bind tighter than + and -, which bind tighter than comparisons, which bind tighter than && and ||. When in doubt, add parentheses — they cost nothing and help the reader.
2 + 3 * 4 // 14
(2 + 3) * 4 // 20
1 + 2 < 4 && true // trueTest yourself
What does 1 + 2 really mean in Scala?
- A built-in instruction that cannot be called otherwise
- The method call
1.+(2) - A macro
- String concatenation
How do you increase a var count by one?
count++++countcount += 1count.inc()
Given val a = new String("x") and val b = new String("x"), what is a == b?
truefalse- A compile error
- It throws