Mathematical tradition, which programming languages generally try to match, dictates that some operations are done before others (for example, multiplication and division are done before addition and subtraction).
a+b*c is the same as
Ever operator has a precedence (a number) associated with it. The precedence determines which operations will be performed first. Multiplication has higher precedence than addition, as illustrated in the previous example..
In addition to the precedence of each operator, the compiler also knows whether equal-precedence operators should be performed left-to-right (almost all) or right-to-left (basically only assignment).
If you have any doubt about the order of evaluation, or have a potentially confusing expression, use parentheses. Remember that one of your goals should be to make your programs as readable as possible. Use parentheses when it makes an expression easier to read, not must when they are absolutely required. Few programmers know the precedence of all operators, so it's common for extra parentheses to be used.
Unary operators have only one operand, for example, in
a = -b;
The "-" operator is the unary (one operand) operator which changes the sign of its operand.
a = b - c
The "-" operator is a binary (two operand) operator which subtracts c from b.
Most unary operators are performed before binary operators (exceptions "." (qualification), "" (subscription), and "()" (method call).
When you can work out the precedence, it's often useful to use parentheses to figure out the order of evaluation. For example, let's say you're evaluating the following expression.
1 + 2 - 3 * 4 / 5
Addition and subtraction are equal in precedence and lower than multiplication and division, which are equal. Form the parenthesized form and work out the values in steps.
1 + 2 - 3 * 4 / 5
= (1 + 2) - ((3 * 4) / 5)
= 3 - (12/5)
= 3 - 2The result of the integer division, 12/5, is 2 .
This table gives the precedence of all operators. You may not be familiar with all operators, but take the advice on the right side and only learn a few precedences.
.  (args) post ++ -- ! ~ unary + - pre ++ -- (type) new * / % + - << >> >>> < <= > >= instanceof == != & ^ | && || ?: = += -= etc
Let's look at the expression
x = a+b-c*d/e, which can be parenthesized as
x = ((a+b) - ((c*d)/e)). Instead of parentheses,
we can draw a diagram.
This dataflow diagram
shows another way to think about expression evaluation.
Boxes represent values in memory, ovals represent operations, and arrows show the direction of data flow. The assignment operator ("=") is treated as an operator with low precedence by the compiler, but from a dataflow point of view it only moves data so it's represented as an arrow, not an operation.
Dataflow diagrams show which operations must necessarily be performed before others, but doesn't show which are actually performed first. Java performs most operations left-to-right, so the addition would, in principle, be performed before the multiplication. I believe reordering of operations that can have no side effects is allowed, however.
Postfix. Altho it's not directly relevant to learning Java, there are other systems for writing expressions that don't need parentheses or precedence. Normal mathematical notation is called infix notation because the operators occur in between the operands. A common alternative is called postfix notation where the operator is written after its two operands.
Example. For example,
a+b would be written as
x = a+b-c*d/e would be written as
RPN. This is often called Reverse Polish Notation in honor of the Polish mathematician Jan Lukasiewicz.
Postfix notation is used in the following, among others.