2 Operators are methods

In Scala, primitives are represented as objects. (Although after compilation they are using Java’s primitives when possible for performance).

Since they are objects, operators are simply method calls!

So 1 + 2 is simply calling a method named + on the object 1 (an Int literal)

e.g.

1 + 2 is actually: (1).+(2)

Scala has precedence for operator-like methods (that will be discussed later) to support correct arithmetic operations.

This notation is called “operator notation” or “infix notation” is not limited just to arithmetic operations, but this will be explained later.


See Also opens in new page

Code editor is using Scalakata.com written by Guillaume Massé