Java: Summary - Expressions

Parentheses () have three uses:
  1. Grouping to control order of evaluation, or for clarity.
    Eg, (a + b) * (c - d)
  2. After a method name to enclose parameters. Eg, x = sum(a, b);
  3. Around a type name to form a cast. Eg, i = (int)x;
Order of evaluation
  1. Higher precedence are done before lower precedence.
  2. Left to right among equal precedence except: unary, assignment, conditional operators.

Abbreviations
i, j - integer (int, long, short, byte, char) values.
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
  1. unary operators
  2. * / %
  3. + -
  4. comparisons
  5. && ||
  6. = assignments
Use () for all others

Arithmetic Operators
The result of arithmetic operators is double if either operand is double, else float if either operand is float, else long if either operand is long, else int.
++iAdd 1 to i before using the value in the current expression
--iAs above for subtraction
i++Add 1 to i after using the value in the current expression
i--As above for subtraction
n + mAddition. Eg 7+5 is 12, 3 + 0.14 is 3.14
n - mSubtraction
n * mMultiplication. Eg 3 * 6 is 18
n / mDivision. Eg 3.0 / 2 is 1.5 , 3 / 2 is 1
n % m  Remainder (Mod) after division of n by m. Eg 7 % 3 is 1
Comparing Primitive Values
The result of all comparisons is boolean (true or false).
== != < <= > >=
Logical Operators
The operands must be boolean. The result is boolean.
b && cConditional "and". true if both operands are true, otherwise false. Short circuit evaluation. Eg (false && anything) is false.
b || c Conditional "or". true if either operand is true, otherwise false. Short circuit evaluation. Eg (true || anything) is true.
!b true if b is false, false if b is true.
b & c"And" which always evaluate both operands (not short circuit).
b | c"Or" which always evaluate both operands (not short circuit).
b ^ c"Xor" Same as b != c
Conditional Operator
b?x:yif b is true, the value is x, else y. x and y must be the same type.
Assignment Operators
= Left-hand-side must be an lvalue.
+= -= *= ...All binary operators (except && and ||) can be combined with assignment. Eg
a += 1 is the same as a = a + 1
Bitwise Operators
Bitwise operators operate on bits of ints. Result is int.
i & jBits are "anded" - 1 if both bits are 1. 5 & 3 is 1.
i | jBits are "ored" - 1 if either bit is 1. 5 | 3 is 7.
i ^ jBits are "xored" - 1 if bits are different. 5 ^ 3 is 6.
~iBits are complemented (0 -> 1, 1 -> 0)
i << jBits in i are shifted j bits to the left, zeros inserted on right. 5 << 2 is 20.
i >> jBits in i are shifted j bits to the right. Sign bits inserted on left. 5 >> 2 is 1.
i >>> jBits in i are shifted j bits to the right. Zeros inserted on left.
Casts
Use casts when "narrowing" the range of a value. From narrowest to widest the primitive types are: byte, short, char, int, long, float, double. Objects can be assigned without casting up the inheritance hierarchy. Casting is required to move down the inheritance hierarchy (downcasting).
(t)xCasts x to type t
Object Operators
co.fMember. The f field or method of object or class co.
x instanceof cotrue if the object x is an instance of class co, or is an instance of the class of co.
a[i]Array element access.
s + tString concatentation if one or both operands are Strings.
x == ytrue if x and y refer to the same object, otherwise false (even if values of the objects are the same).
x != yAs above for inequality.
comparisonCompare object values with .equals() or .compareTo()
x = yAssignment copies the reference, not the object.

Copyright 2005 Fred Swartz