*, /, and % are done before + and -. Parentheses go first of all. Operators of equal precedence are done left to right.
*
/
%
+
-
System.out.println(2 + 3 * 4); // 14 (multiply first) System.out.println((2 + 3) * 4); // 20 (parentheses first) System.out.println(10 / 2 * 5); // 25 (left to right) System.out.println(8 - 3 - 2); // 3 (left to right)
▶ Try it: run this, then change something and run it again.
What is 2 + 3 * 4?
2 + 3 * 4
What is (2 + 3) * 4?
(2 + 3) * 4
Type exactly what this prints:
System.out.println(10 - 2 * 3);
What is 10 / 2 * 5?
10 / 2 * 5
Java evaluates 2 + 3 * 4 - 1 step by step. Put the steps in the order Java performs them.
2 + 3 * 4 - 1