random important facts Flashcards
java precedence rules (high to low)
parentheses:( ) unary ops: +x -x ++x –-x x++ x-- !x multiply/divide: * / % add/subtract: + - comparisons: < > <= >= equality: == != logical and: && logical or: || assignments:= += *= /= %= (these are right to left associative)
short-circuiting
As soon as Java knows an answer, it quits evaluating the expression (regardless of what the 2nd expression is)
is (++x==0) executed?
int x = 0, y = 1;
if ((y > 1) && (++x == 0)){
}
no, because (y>1) is false, java stops evaluating the expression, before it reaches the 2nd part.
break
allows you to exist immediately from a loop
(while, do-while, for)
(only use break when loop condition is always true)
continue
jumps to the bottom of the loop immediately ( } ), and starts the next iteration
math.
math. provides static services
methods: math.add, math.ceil, math.floor, math.pow, math.random (values btwn 0-1
switch statement
replacement for cascading if-else statements (if tested expression is an integer, string, or enum)
switch statement code
switch (〈control-expression〉) { case〈case-label-1〉: 〈statement-sequence-1〉 break; case〈case-label-2〉: 〈statement-sequence-2〉 break; … case〈case-label-n〉: 〈statement-sequence-n〉 break; default:〈default-statement-sequence〉 break; }
control expression
can be of one of the following types:
char, int, short, byte, String, enum
“break” statement
jumps out of the switch statement. Otherwise control
flow just “falls through” into the next case
(falling though
behavior is handy, because it allows you to combine cases)
floating point
you can never use == to compare floating points. Check if 2 numbers are within a certain tolerance of each other:
Math.abs((3.9-3.8) - 0.1)