Operator Precedence in Java


TL ; DR:

  • When we string operations together, Java must know which one to do first. This is called operator precedence.


  • * and / are performed before + and -:

  • System.out.println(1 + 2 * 3); // Prints 7
    System.out.println(6 - 4 / 2); // Prints 4
    System.out.println(5 * 4 - 2 / 2); // Prints 19
    

  • We can add () to force an operation to be performed first:

    System.out.println((1 + 2) * 3); // Prints 9
    System.out.println((6 - 4) / 2); // Prints 1
    System.out.println(5 * (4 - 2) / 2); // Prints 5
    




Full lesson:

When we string operations together, Java must know which one to do first. This is called operator precedence.

This is the hierarchy from highest precedence to lowest precedence:

  • Parentheses are always respected
  • Exponentiation (raise to a power)
  • Multiplication, Division and Remainder
  • Addition and Subtraction
  • Left to right


First example:

System.out.println(1 + 2 * 3); // Output: 7

Multiplication is executed before addition, so:

1 + 2 * 3 = 1 + 6 = 7

Second example:

System.out.println((1 + 2) * 3); // Output: 9

What's inside parentheses is executed before multiplication, so:

(1 + 2) * 3 = 3 * 3 = 9

Third example:

System.out.println(6 / 4 * 3 + 5); // Output: 9.5

Multiplication and division come before addition. Also, multiplication and division have the same priorty so they are executed from left to right:

6 / 4 * 3 + 5 = 1.5 * 3 + 5 = 4.5 + 5 = 9.5

Forth example:

System.out.println(6 / ((4 + 2) / 3)); // Output: 3

Computer sees parentheses so it looks at it like this:

6 / ((4 + 2) / 3) = 6 / x, where x = (4 + 2) / 3

I have to compute x first. Parantheses are executed before division:
(4 + 2) / 3 = 6 / 3 = 2

I can replace x with 2:
6 / ((4 + 2) / 3) = 6 / 2 = 3


Assignment
Follow the Coding Tutorial and let's practice with operator precedence!


Hint
Look at the examples above if you get stuck.


Introduction

In Java, when multiple operations are combined in a single expression, the order in which these operations are performed is determined by operator precedence. Understanding operator precedence is crucial for writing correct and efficient code. It ensures that expressions are evaluated in the intended order, avoiding logical errors and unexpected results.

Operator precedence is particularly useful in scenarios involving complex mathematical calculations, conditional statements, and logical operations. It helps in simplifying expressions and making the code more readable and maintainable.

Understanding the Basics

Operator precedence defines the rules that determine the order in which different operators in an expression are evaluated. Operators with higher precedence are evaluated before those with lower precedence. If operators have the same precedence, their associativity (left-to-right or right-to-left) determines the order of evaluation.

For example, in the expression 1 + 2 * 3, the multiplication operator (*) has higher precedence than the addition operator (+). Therefore, the multiplication is performed first, resulting in 1 + 6, which equals 7.

Main Concepts

Let's delve into the key concepts and techniques involved in operator precedence:

Examples and Use Cases

Let's look at some examples to understand how operator precedence works in different contexts:

Example 1

System.out.println(1 + 2 * 3); // Output: 7

Explanation: Multiplication is performed before addition, so the expression is evaluated as 1 + 6, resulting in 7.

Example 2

System.out.println((1 + 2) * 3); // Output: 9

Explanation: Parentheses have the highest precedence, so the expression inside the parentheses is evaluated first, resulting in 3 * 3, which equals 9.

Example 3

System.out.println(6 / 4 * 3 + 5); // Output: 9.5

Explanation: Division and multiplication are performed before addition. The expression is evaluated as 1.5 * 3 + 5, resulting in 4.5 + 5, which equals 9.5.

Example 4

System.out.println(6 / ((4 + 2) / 3)); // Output: 3

Explanation: Parentheses are evaluated first, followed by division. The expression inside the parentheses is evaluated as (4 + 2) / 3, resulting in 2. The final expression is 6 / 2, which equals 3.

Common Pitfalls and Best Practices

When working with operator precedence, it's important to be aware of common mistakes and follow best practices:

Advanced Techniques

In addition to basic operator precedence, there are advanced techniques that can be used to optimize and refactor code:

Code Implementation

Here are some well-commented code snippets demonstrating the correct use of operator precedence:

// Example 1: Basic operator precedence
System.out.println(1 + 2 * 3); // Output: 7

// Example 2: Using parentheses to change precedence
System.out.println((1 + 2) * 3); // Output: 9

// Example 3: Combining division, multiplication, and addition
System.out.println(6 / 4 * 3 + 5); // Output: 9.5

// Example 4: Nested parentheses
System.out.println(6 / ((4 + 2) / 3)); // Output: 3

Debugging and Testing

Debugging and testing are essential for ensuring the correctness of expressions involving operator precedence:

Example test case:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class OperatorPrecedenceTest {
    @Test
    public void testBasicPrecedence() {
        assertEquals(7, 1 + 2 * 3);
    }

    @Test
    public void testParenthesesPrecedence() {
        assertEquals(9, (1 + 2) * 3);
    }

    @Test
    public void testCombinedOperations() {
        assertEquals(9.5, 6 / 4.0 * 3 + 5, 0.0001);
    }

    @Test
    public void testNestedParentheses() {
        assertEquals(3, 6 / ((4 + 2) / 3));
    }
}

Thinking and Problem-Solving Tips

When approaching problems related to operator precedence, consider the following strategies:

Conclusion

Mastering operator precedence is essential for writing correct and efficient Java code. By understanding the rules of precedence and applying best practices, you can avoid common pitfalls and create more readable and maintainable code. Practice regularly and explore advanced techniques to further enhance your skills.

Additional Resources

For further reading and practice problems, consider the following resources: