Short-Circuiting
Java has short circuiting for the && and || operators.
Keep this in mind in exercises that have a compile error on the right side of this operator.
This means that p || q and p && q is not commutative. This also means that side-effects of the symbols might not get executed if evaluation is short-circuited:
a != 0 && b / a does not error out since the check for zero makes the expression short-circuit.
Method Overloading
Defining multiple methods with the same signature is not allowed:
- same types just different names
- different return types (this doesn’t allow for choosing the correct one)
If you do overload a function name, the one with the most-matching signature will be called.
Prefix and Postfix increment and decrement
The result of
int a = 1;
int b = ++a; (b = 2, a = 2)
is different from the result of
int a = 1;
int b = a++; (b = 1, a = 2)
Because the ++ and -- operators either run before the assignment or after depending on position.
Do-While Loops
do {
// body
} while (test);
statement;First the body is executed, then the test is performed. If the test fails, we continue at statement, otherwise we loop the body once more.