Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question #3 from Doug's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question #3 from Doug Watch "Question #3 from Doug New topic
Author

Question #3 from Doug's book

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If an instanceof comparison returns false then a similar cast will throw a ClassCastException. For example:
Object obj = new java.awt.Button();
boolean b = obj instance of String; //b is equal to false
String s = (String)obj; // throws ClassCastException
There is one and only one exception to this rule. What is it?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Thomas Paul:
If an instanceof comparison returns false then a similar cast will throw a ClassCastException. For example:
Object obj = new java.awt.Button();
boolean b = obj instance of String; //b is equal to false
String s = (String)obj; // throws ClassCastException
There is one and only one exception to this rule. What is it?

null ? Because it can be cast into anything?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, that's it. Beat me to it


[Jess in Action][AskingGoodQuestions]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hey Thomas I like you questions a lot. I think they are helping many Ranchers.


SCJP2. Please Indent your code using UBB Code
Jui Mahajan
Ranch Hand

Joined: Jun 02, 2003
Posts: 62
how come null can be cast to anything ? I thought thatusing instanceof operator on a null would always return false ? Therefore, null cannot be cast to any object. ALso, I had thought that equals() method returns false on null; i.e. anyobject.equals (null) is always false .
AM i right...pls clarify.


-----jui<br />scjp1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
According to JLS 5.1.4 Widening reference conversions there ia a widening reference conversion "From the null type to any class type, interface type, or array type."
Also from JLS 5.5
"The cast can be determined to be correct at compile time. A cast from the compile-time type S to compile-time type T is correct at compile time if and only if S can be converted to T by assignment conversion (�5.2).
"
And finally from JLS 5.2
"Assignment contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), or a widening reference conversion (�5.1.4). "
hope it helps
[ August 10, 2003: Message edited by: Jose Botella ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Since a null object always casts cleanly, that is the one exception to the rule about instanceof and ClassCastException.
Object obj = null;
boolean b = obj instance of String; //b is equal to false
String s = (String)obj; // executes without Exception
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question #3 from Doug's book