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 |
The result of all comparisons is boolean (true or false). |
== != < <= > >= |
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 |
b?x:y | if b is true, the value is x, else y.
x and y must be the same type. |
|
= | 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 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. |
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 |
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. |
|