Arithmetic and precedence
Java calculates with the usual operators: + adds, - subtracts, * multiplies and / divides. A calculation can use numbers, variables, or both, and its result can be stored in a variable:
int minutes = 3 * 60;
int total = minutes + 45;
System.out.println(total);
225Which part first
As in maths, * and / are done before + and -. Parentheses change the order: whatever is inside them is calculated first.
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
14
20A piece of code that has a value, like 2 + 3 * 4 or minutes + 45, is called an expression. A statement such as int total = minutes + 45; calculates the expression and does something with its value, here storing it.