aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes null & instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null & instanceof" Watch "null & instanceof" New topic
Author

null & instanceof

Akash Singhal
Greenhorn

Joined: Feb 12, 2005
Posts: 11
Hi,



Returns false

However
prints null.

Besides null also gets typecast into any user-defined objects inspite of
instanceof returning false.. how is it possible???Thanking in advance

Regards,
Abhinay Verma
Greenhorn

Joined: Dec 13, 2004
Posts: 24
null can be cast to any reference type without any side-effects. However, null is never an instance of any class. Therefore,
null instanceof Object will return false
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The "<reference> instanceof <class>" operator works on the actual instance referred to by <reference>. Since a null reference has no class, it cannot be an instace of any class, and it always returns false.

[ Disabled smilies. ]
[ February 21, 2005: Message edited by: David Harkness ]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
System.out.println(String(null));

This will give compile time error . What you want to do ?
Akash Singhal
Greenhorn

Joined: Feb 12, 2005
Posts: 11
No Rathi,
System.out.println(String(null)) prints null onto the screen.
System.out.println(null) gives compile time error.Please check..

Regards
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Rathi is correct.

System.out.println(String(null))

Compile-time error.


System.out.println(null)

Prints literal "null"

Please check.
[ February 21, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Akash Singhal:
System.out.println(String(null)) prints null onto the screen.
This is definitely a compile-time error. Do you want to cast the null reference to a String type?
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200

prints the string null

compile time error


Be Afraid...Be very Afraid...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null & instanceof
 
Similar Threads
null instanceof Object in S.O.P
'instanceof' operator
Doubt
instanceof doubt?
Inheritance-1