Variables Flashcards
An int always takes ___ bytes
4
A fraction multiplied by an integer returns ____
zero
To avoid truncation (operation using two integers)….
cast one of them to a double BEFORE the operation is applied (i.e. “a / (double) b” NOT “(double) (a/b)*. If at least one of the operands is a double, there is no need to cast the other one–it is promoted to a double automatically
Simply saying (1/2) will return what?
- This is because when one divides two integers, the resulting double is rounded DOWN to an integer
== vs. .equals
== is true if and only if the two variables refer to exactly the same object (ADDRESSES). .equals compares the CONTENTS of the two objects (used for strings, primarily)
De Morgan’s Laws
!(a && b) is the same as !a || !b
!(a || b ) is the same as !a && !b
Short-circuit evaluation
If the value of the first operand is sufficient to determine the result, then the second operand is NOT evaluated. For example:
int x = 0, y = 3;
String op = “/”
if (op.equals(“/”) && (x!= 0) && (y/x > 2)
do stuff
else
do other stuff
This code WILL compile and execute without error because although y/x is undefined, we never get that far b/c the second operand fails