Monday, 9 November 2009

SCJP Sun Certified Programmer By Kathy Sierra and Bert Bates - Part4

Couple of weeks back i started practicing SCJP Sun Certified Programmer for Java 5 Study Guide By Kathy Sierra and Bert Bates.

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
nullAny class or interface typefalse
Foo instanceFoo, Bar, Face, Objecttrue
Bar instanceBar, Face, Objecttrue
Bar instanceFoofalse
Foo[]Foo, Bar, Facefalse
Foo[]Objecttrue
Foo[]Foo, Bar, Face, Objecttrue


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: