# Java: Summary - Expressions

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. Eg, i = (int)x; Order of evaluation Higher precedence are done before lower precedence. 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 unary operators * / % + - comparisons && || = 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.
`++i` | Add 1 to i before using the value in the current expression
`--i` | As above for subtraction
`i++` | Add 1 to i after using the value in the current expression
`i--` | As above for subtraction
`n + m` | Addition. Eg 7+5 is 12, 3 + 0.14 is 3.14
`n - m` | Subtraction
`n * m` | Multiplication. Eg 3 * 6 is 18
`n / m` | Division. 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 && c` | Conditional "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:y | if 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 & j | Bits are "anded" - 1 if both bits are 1. 5 & 3 is 1.
i | j | Bits are "ored" - 1 if either bit is 1. 5 | 3 is 7.
i ^ j | Bits are "xored" - 1 if bits are different. 5 ^ 3 is 6.
~i | Bits are complemented (0 -> 1, 1 -> 0)
i << j | Bits in i are shifted j bits to the left, zeros inserted on right. 5 << 2 is 20.
i >> j | Bits in i are shifted j bits to the right. Sign bits inserted on left. 5 >> 2 is 1.
i >>> j | Bits 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)x | Casts x to type t
Object Operators
co.f | Member. The f field or method of object or class co.
x instanceof co | true 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 + t | String concatentation if one or both operands are Strings.
x == y | true if x and y refer to the same object, otherwise false (even if values of the objects are the same).
x != y | As above for inequality.
comparison | Compare object values with .equals() or .compareTo()
x = y | Assignment copies the reference, not the object.

Copyright 2005 Fred Swartz