File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException & instanceof operator 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 "ClassCastException & instanceof operator" Watch "ClassCastException & instanceof operator" New topic
Author

ClassCastException & instanceof operator

R Leigh
Greenhorn

Joined: Apr 04, 2013
Posts: 13
When is ClassCastException thrown? And when it is just a compiler error?
How does using instanceof operator come in the picture?

I've been reading many examples - some of which throw exception on downcasting and others compile error.

Is this right:
(Here by illegal downcasting I mean classes which are not from the same inheritance tree)

1) If you don't use instanceof operator, and if you downcast illegally then compile error will be thrown.
2) If you do use instanceof operation, and if you downcast illegally then ClassCastException will be thrown but the code will compile.
3) If you downcast legally (ie belonging to the same inheritance tree) then you'll be able to compile and run irrespective of whether you use instanceof operator.

Please help me out. I am massively confused.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

R Leigh wrote:When is ClassCastException thrown? And when it is just a compiler error?
How does using instanceof operator come in the picture?

I've been reading many examples - some of which throw exception on downcasting and others compile error.

Is this right:
(Here by illegal downcasting I mean classes which are not from the same inheritance tree)

1) If you don't use instanceof operator, and if you downcast illegally then compile error will be thrown.
2) If you do use instanceof operation, and if you downcast illegally then ClassCastException will be thrown but the code will compile.
3) If you downcast legally (ie belonging to the same inheritance tree) then you'll be able to compile and run irrespective of whether you use instanceof operator.

Please help me out. I am massively confused.



* A ClassCastException is thrown when you try to cast an object to something that it is not.
* It is a compile error, if the compiler can figure out (at compile time obviously) that there is no way that the cast can succeed.
* The instanceof operator can be used before you cast, in order to avoid getting a ClassCastException thrown -- basically, if it is not an instance of the class that you want, then don't cast it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
R Leigh
Greenhorn

Joined: Apr 04, 2013
Posts: 13
Thank you Henry.

Can you please clear this up for me?

If I'm downcasting correctly and legally without using instanceof operator... then the code will compile and run, right?

So, essentially we use instanceof operator if we're not sure whether the class we're casting the object to belongs to the same inheritance tree or not.

ie if I'm sure that the two classes in question belong to the same inheritance tree then using instanceof operator is of no value, right?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

R Leigh wrote:
Is this right:
(Here by illegal downcasting I mean classes which are not from the same inheritance tree)


Interesting definition... what about interfaces? Is that a legal cast or not?

R Leigh wrote:
1) If you don't use instanceof operator, and if you downcast illegally then compile error will be thrown.


If the compiler can detect that there is no way for the cast to succeed, then it is a compile error. And it doesn't matter if your code is using the instanceof operator or not.

R Leigh wrote:
2) If you do use instanceof operation, and if you downcast illegally then ClassCastException will be thrown but the code will compile.


Same answer.

R Leigh wrote:
3) If you downcast legally (ie belonging to the same inheritance tree) then you'll be able to compile and run irrespective of whether you use instanceof operator.


No. A ClassCastException will be thrown when you try to cast an object to something that it is not.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

R Leigh wrote:
If I'm downcasting correctly and legally without using instanceof operator... then the code will compile and run, right?

So, essentially we use instanceof operator if we're not sure whether the class we're casting the object to belongs to the same inheritance tree or not.

ie if I'm sure that the two classes in question belong to the same inheritance tree then using instanceof operator is of no value, right?



If you are sure that an object is the type of class that you are casting to, then using the instanceof operator is not needed.


Not sure what you mean by "correctly and legally". Can you elaborate with an example?

Henry
R Leigh
Greenhorn

Joined: Apr 04, 2013
Posts: 13
I think what I am not getting is this... when will the compiler be able to detect that there is no way for the cast to succeed and when will it won't be able to?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

R Leigh wrote:I think what I am not getting is this... when will the compiler be able to detect that there is no way for the cast to succeed and when will it won't be able to?



For the simple case of classes, then yes, you are correct -- the compiler will detect that you can't cast an object referred to by StringBuffer reference, to a String object (the hierarchy reason). It just get a bit complex, when interfaces are thrown into the works. Personally, I think you should work on figuring out casting first -- you need to understand that you can't cast an object to a class type that it is not. This is true regardless of the class hierarchy or whether you use the instanceof (or any other) operator.

Henry
R Leigh
Greenhorn

Joined: Apr 04, 2013
Posts: 13
Thanks a lot Henry! Really very helpful. I'll certainly read more to try to understand "casting" to the core.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
Let me try an example :
R Leigh
Greenhorn

Joined: Apr 04, 2013
Posts: 13
@Himai

Thank you so so so much! Finally I got it!
sakthi vandhana
Greenhorn

Joined: Aug 08, 2013
Posts: 9


Output
====

B's own fn
B's own fn


Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException & instanceof operator
 
Similar Threads
Downward casting
instanceof doubt?
instanceof key word
reference variable downcasting
Another examLab casting question