Great book written by Kathy Sierra and Bert Bates.
I wanted to share few important quotations found from the fourth chapter.
1) It is legal to test whether the null reference is an instance of a class. This will always result in false, of course. For example:
class InstanceTest {
public static void main(String [] args) {
String a = null;
boolean b = null instanceof String;
boolean c = a instanceof String;
System, out.println (b + " " + c) ;
}
}
prints this: false false
2) Below table summarizes the use of the instanceof operator given the following:
interface Face { }
class Bar implements Face{ }
class Foo extends Bar { }
| First Operand (Reference Being Tested) | instanceof Operand (Type We're Comparing the Reference Against) | Result |
|---|---|---|
| null | Any class or interface type | false |
| Foo instance | Foo, Bar, Face, Object | true |
| Bar instance | Bar, Face, Object | true |
| Bar instance | Foo | false |
| Foo[] | Foo, Bar, Face | false |
| Foo[] | Object | true |
| Foo[] | Foo, Bar, Face, Object | true |
3) The || and && operators work only with boolean operands.
4) Difference between short-circuit and non-short-circuit logical operators.
int z = 5;
if(++z > 5 | | ++z > 6) z++; // z = 7 after this code
versus:
int z = 5 ;
if (++z > 5 | ++z > 6) z++; // z = 8 after this code
5) There are six relational operators: >, >=, <, <= , ==, and !=. The last two ( == and ! = ) are sometimes referred to as equality operators.
6) instanceof is for reference variables only, and checks for whether the object is of a particular type.
7) The || does not evaluate the right operand if the left operand is true.
8) The & and | operators always evaluate both operands.
No comments:
Post a Comment