The "shortcircuit" form of "Boolean and" (&&) comes in handy when evaluating all of the arguments in an "if" statement could potentially throw an exception. Consider the
following example:
// Declaration.
Book b;
// details omitted
if (b != null && b.getTitle().equals("Beginning
Java Objects")) {
// details omitted
}
In the above example, if reference variable "b" happens to
never have been initialized to refer to a Book object, b will be equal to null. Using the && operator, we'll discover that b IS null, and will avoid evaluating the second half of the expression, which would throw a NullPointerException when we try to invoke the getTitle() method on the non-existent b object. Only if the first clause is true -- namely, b is NOT null -- will the second clause be executed.
On the other hand, had we written:
if (b != null & b.getTitle().equals("Beginning Java Objects")) {
then BOTH clauses would be evaluated regardless of the value of the first clause, and a NullPointerException would be thrown.
Hope this helps!
[This message has been edited by Jacquie Barker (edited December 20, 2000).]