Java Glossary : precedence

CMP home Java glossary home Menu no menu Last updated 2004-06-29 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : P words : precedence.

precedence
Java inherits a very complicated operator precedence scheme from C. Operators with lower precedence numbers are done first. The advantage of precedence is it means you need fewer parentheses. The disadvantage is you must memorise this table perfectly to understand what code will do. Once you learn this table you will know if:

x = a | b & c;

means:

x = (a | b) & c;

or

x = a | (b & c);
Operators near the top of the table are done first. The lower the precedence number, the eariler the operations are done. Equal precedence operations usually are evaluated left to right, except for ones with right association, which are evaluated right to left. What a hodge podge!
Operator Precedence
Precedence Operator Association Notes
1 (prefix) ++ --
(unary) + - ~ !
(cast)
Right (prefix) ++ prefix means preincrement, . ~ is bitwise not for ints. ! is logical not for booleans. Nearly always, you have to put the expression after ! in parentheses. You might as well make a habit of always doing it.
1 (postfix) ++ --
Right (postfix) ++ postfix means postincrement
2 * / % Left (infix) % is modulus, remainder. / is integer division for ints and floating point division for doubles.
3 + - Left (infix) a - b - c means (a - b) - c not a - ( b - c ), additive operations are performed left to right. + also means concatenation.
4 << >> >>> Left (infix) There is no <<< operator because it would be identical to <<. You have to keep your wits about you when doing unsigned shifts to remember all right shifts must be done with >>>.
5 < > <= >=
instanceof
Left (infix)  
6 == != Left (infix) Pascal's <> not equal will not work. == and != work on booleans too, often saving a forest of if/elses.
7 & Left (infix) Bitwise AND mostly for for ints.
8 ^ Left (infix) XOR for ints. It is the difference operator. It is true if the boolean operands are different. e.g.

false ^ false == false
false ^ true == true
true ^ false == true
true ^ true == false

It is useful in cryptography because of this magic property of encryption and decryption with a random scrambler number.

long encrypted = message ^ scrambler;
long decryped = encrypted ^ scrambler;

If you XOR twice with the scrambler, you get right back where you started. For booleans it is clearer to use a != b instead of a ^ b and a == b instead of !( a ^ b)

9 | Left (infix) bitwise OR mostly for ints.
10 && Left (infix) short circuit logical AND for booleans.
11 || Left (infix) short circuit logical OR for booleans.
12 ? : Right ( ternary ) a = b ? c : d; is shorthand for if ( b ) a = c; else a = d;
13 = *= /= += -=
<<= >>= >>>=
&= ^= |=
Right (infix) These make proofreading easier by eliminating typing a variable name twice.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 2279.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/precedence.html J:\mindprod\jgloss\precedence.html