Parentheses () have three
uses:
- Grouping to control order of evaluation, or for clarity.
Eg, (a + b) * (c - d)
- After a method name to enclose parameters. Eg, x = sum(a, b);
- Around a type name to form a cast operator. Eg, i = (int)x;
Order of evaluation
- Higher precedence are done before lower precedence (see table).
- Left to right among equal precedence except: unary, assignment
and conditional operators.
Abbreviations used below
i, j - integer (int, long, short, byte, char).
m, n - numeric values (integers, double, or float).
b, c - boolean; x, y - any primitive or object type.
s, t - String; a - array; o - object; co - class or object |
Operator Precedence |
. [] (args) post ++ -- ! ~ unary + - pre ++ -- (type) new * / % + - << >> >>> < <= > >= instanceof == != & ^ | && || ?: = += -= etc
|
Remember only:
- unary operators
- * / %
- + -
- comparisons
- && ||
- = assignments
Use () for all others |
|