Thursday 20 September 2007

To be null or not to be ?


Book myBook = getBookFromSomeWhere();
System.out.println("myBook: " + myBook);

if (myBook == null) {
System.out.println("myBook is null");
}
else {
System.out.println("myBook is not null");
}


Run it ! And that display :

myBook: null
myBook is not null


How is it possible ?

Hahaha.

It is because we have:

Book {
...

public String toString() {
return "null";
}


Funny, isn't it ?