Originally posted by Dean Jones:
When the following lines are printed,different outputs are obtained.
The output is true.
The output is false.
Can anyone please explain.
Hi Dean,
The code referred to by "line 1" returns true since an empty
String to the left of "+" operator caused it to be overloaded and concatanates with null making it a String. As all objects in Java inherit from java.lang.Object the instanceof will return true.
The "line 2" however directly tests if a null reference derives from the Object or is a subclass of it however sice it does not refer to any object it by no means can be any of them. That's why the instanceof returns false.