Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Operators Precedence in Scala

In Scala, operator precedence determines the way operators are parsed concerning each other. The precedence is determined by the first character of the operator. The following table presents the precedence levels in descending order, with operators on the same line having the same precedence:

  1. (all other special characters)
  2. * / %
  3. + -
  4. :
  5. = !
  6. < >
  7. &
  8. ^
  9. |
  10. (all letters)
  11. (all assignment operators)

Here are some rules and notes based on the table:

  1. Higher to Lower: Operators at the top of the list have higher precedence than those at the bottom.

  2. Assignment: Assignment operators have the lowest precedence. All assignment operators in Scala (=, +=, -=, etc.) have the same precedence.

  3. Method Names as Operators: Since operators are method calls in Scala, the precedence rule applies to method names as well. For example, a method named *** will have higher precedence than a method named +++.

  4. Right Associativity: If an operator ends in a colon :, it has right associativity. Otherwise, it has left associativity. Right-associative operators appear to bind to the right:

    val result = 1 :: 2 :: List(3, 4)
    // This is equivalent to: 1 :: (2 :: List(3, 4))
    
  5. Letters: All letters have the same precedence. They have lower precedence than most special characters but have higher precedence than assignment operators.

  6. Special Characters: If an operator doesn't start with a letter or an assignment character, then its precedence is determined by its first character, based on the order in the table.

Remember, when in doubt or when you think an expression might be ambiguous to readers, use parentheses to clarify the order of operations. It enhances readability and removes any potential confusion.

  1. Mathematical Operators Precedence in Scala:

    In Scala, mathematical operators follow the usual order of precedence, similar to standard mathematics. Operators like *, /, % have higher precedence than + and -.

    val result = 2 + 3 * 4 / 2 // Result will be 8 (2 + (3 * (4 / 2)))
    
  2. Comparison Operators Precedence in Scala:

    Comparison operators, such as <, >, <=, >=, have higher precedence than logical operators.

    val comparisonResult = (5 > 3) && (2 <= 4) // Result will be true
    
  3. Logical Operators Precedence in Scala:

    Logical operators like && (AND) and || (OR) have lower precedence than comparison operators.

    val logicalResult = (5 > 3) || (2 <= 1) && (4 == 4) // Result will be true
    
  4. Bitwise Operators Precedence in Scala:

    Bitwise operators, such as &, |, and ^, have lower precedence than logical operators.

    val bitwiseResult = (3 & 1) | (5 ^ 2) // Result will be 7 ((3 AND 1) OR (5 XOR 2))
    
  5. Precedence of Assignment Operators in Scala:

    Assignment operators, like =, have lower precedence than most other operators.

    var x = 5
    x += 3 * 2 // x is now 11 (5 + (3 * 2))
    
  6. Custom Operators Precedence in Scala:

    Custom operators, defined by methods, follow the same precedence rules as built-in operators.

    class MyNumber(value: Int) {
      def **(power: Int): Int = scala.math.pow(value, power).toInt
    }
    
    val result = new MyNumber(2) ** 3 // Result will be 8 (2^3)
    
  7. Changing Operator Precedence in Scala:

    Scala allows you to change operator precedence by using parentheses.

    val changedPrecedence = 2 + 3 * 4 // Result is 14 (2 + (3 * 4))
    val newPrecedence = (2 + 3) * 4 // Result is 20 ((2 + 3) * 4)