Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

When instanceof will return ClassCastException?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started to get confused when I saw this snipped in page 36 of OCP guide:


The if comparison compiles but I think it shouldn't or I'm missing something (probably the second option, that's why I posting this),
because obj is of type Object, and it can't be casted to String, or be compatible in any way with String.

The only ways I could think was changing the sentence.. I couldn't think nothing at compilation time to make this return true, without changing the sentence..

So, why is it returning false, instead of throwing a ClassCastException?

Thanks in advance
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thiago, welcome to the Ranch!

Thiago Medeiros wrote:...because obj is of type Object...



You don't know that. Not exactly, anyway. All you know is that obj refers to some object which can be cast to type Object. So yes, it's fair to say that it's of type Object but it could be of any other type also. It could be of type Book, for example.

So, why is it returning false, instead of throwing a ClassCastException?



Because that's what the "instanceof" operator does. The designers of Java rightly decided that there was a need for finding out whether an object could be cast to a certain type. The example you posted is an example where that need exists. But having the operator return "true" if it could be, and throwing an exception if it couldn't be, that wouldn't be a good design. Don't you agree?
 
Marshal
Posts: 65062
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

What type s the ISBN field? (That should read isbn to maintain the normal naming conventions.) It shouldn't be an int or a long because ISBNs aren't numbers in the sense that you never do arithmetic with ISBNs. In which case the == operator will give you incorrect results.
As Paul has already told you, instanceof tests whether the runtime type of obj has Book in its inheritance tree, either as its type or a supertype. The only place I can think of to use instanceof without thinking you have got your design wrong is in the equals() method.http://www.coderanch.com/how-to/java/AvoidTheEqualityOperator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!