Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof and casts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof and casts" Watch "instanceof and casts" New topic

instanceof and casts

Anna Dow

Joined: Jun 04, 2002
Posts: 18
Hi, all,
here is my question:
It looks that if returned true, then the cast is always possible.
(That is, the code like:

will never give a ClassCastException.) Is it true ?
Thanks, Anna
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
Yeah thats true.
But the code which you gave wont compile
coz you didn't use the if statement properly

should be made

Don't get me started about those stupid light bulbs.
subject: instanceof and casts
It's not a secret anymore!