Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

ClassCastError vs. Compilation Error

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When is there a ClassCastError or a CompilationError? Are there some rules? Are there some more cases as I list below?

If I have two sibling objects (objects with the same parent) as in #1 there is a compilation error, this means, that the compiler can foresee that something is wrong.

If I have a polymorphic declaration Parent p=new Child and I do not cast in the IS-A direction (correct would be: Double result=(Double)cce;) a ClassCastException is thrown at runtime.

It is somehow clear for me, that you cannot cast a Double into a String, but what is the cue behind this?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think because String and Object are in the same inheritance tree, so comiple with no error. If you change

to

and uncomment the cast statement, it will compile but raise ClassCastException at runtime.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terence Gronowski wrote:When is there a ClassCastError or a CompilationError? Are there some rules? Are there some more cases as I list below?



Hi Terence, please refer to this thread for casting rules: - webpage

If I have two sibling objects (objects with the same parent) as in #1 there is a compilation error, this means, that the compiler can foresee that something is wrong.



if you apply referencing rule here. ("A reference of type X can point to objects of class X, or to objects of any of X's subtypes. "
then this can be true

now downcasting is required when you assign "reference of type B" (e.g B b) to a "reference that is supertype of B" (e.g a)"
i.e.

and in your case d2 is not a supertype of d1!

It is somehow clear for me, that you cannot cast a Double into a String, but what is the cue behind this?



That is again referencing rule - "if the object pointed to by the reference on right hand side(e.g a) is not assignment compatible with the reference type you are assigning to (e.g b) according to the referencing rule, a ClassCastException will be issued at run time"

and the complete (compiled code) is


I hope this would help..
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks Jia and Vishal

I remembered a much simpler rule:

First: Does it compile? Is the inheritance tree ok?



Here the inheritance tree is not ok, no compile!

----------------


Second: If I have a downcast without a foregoing upcast there will be a ClassCastException



In the above example, the inheritance tree is ok. But if d1=d2, the upcast, is whiped out, there is the class cast exception.

What do I with d1=d2? I put the broader d2 reference in the narrower type, so that I have all the information of d2.

I am just studiing your checking rules with mine....what are the differences?



 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Applying the rule "no Downcast without prior Upcast" in a trap, the scjp-trappers would have ready for us:



Strategy:

1. Is the ineritance tree ok, if yes proceed, the code compiles, if no=compilation error
2. All upcasts will run ok (Parent=Child), also nonsense casts as in e), a) b) and e) will run
3. Are there any downcasts (Child=Parent)? if yes beware, in c) and d) there are
4. Is there any upcast before the downcast? Has the Child-Constructor ever run before on the parent-reference? If yes ok, if no =>ClassCastException
in c) the foregoing upcast lacks!

Is this a save procedure not not beeing predated?



 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...it is not. There are some wired possibilities, as composing types and references in a "wild" way:

in f) there ist a compiler error
g) runs ok



It seems not so easy to derive a fool proof strategy for this sort of problems!
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terence this code is not giving any compilation error (as it should)



I don't understand what you are confused with. Can you please explain more clearly your confusion...
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh sorry, it is a ClassCastError as it should be. It is not a compilation error.

I want to have some reliable rules to for the questions "does it compile" or "there is a ClassCastError".

My example should be a recipe how to do that. Here is it again:

a) is the inheritance sequence ok, if yes proceed, if no => compilation error
b) is there any downcasting? no => it should run, yes => proceed
c) is there an upcast on the reference variable which is involved in the downcast? => yes => runs
d) If ther is no preceeding upcast => ClassCastError

Does that recipe work?
 
Alas, poor Yorick, he knew this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic