Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCast Exception..

 
Rekha Rao
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the following should work. But, I get a ClassCastException in I, whereas II works fine. What's the difference between I and II ?
class Test{}
class Test1 extends Test{}
I.
<some method>....
Test t[] = {new Test1(), new Test1()};
//ClassCastException, here:
Test1 t1[] = (Test1[])t;
II.
<some method>....
Test t[];
Test1 t1[] = {new Test1(), new Test1()};
t = t1;
Test1 t11[] = (Test1[])t;
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rekha,
You must think in terms of memory size in this case. To make the explanation easier to understand lets say the following:
Test takes 2 bytes of memory
Test1 takes 4 bytes of memory (because it extends Test)
Your first example declares an array reference of Test items. This means that after the line:
Test t[] = {new Test1(), new Test1()};
we have an array pointing to the following:
t[0] --> 2 bytes (only sees the Test part of Test1
t[1] --> 2 bytes (only sees the Test part of Test1
Then you are trying to cast the 2 bytes into 4 bytes. Can't do that because we don't have the extra 2 bytes to get ...
****************
The second case creates an array of Test1 elements. Therefore after the line:
Test1 t1[] = {new Test1(), new Test1()};
we have an array of elements that:
t1[0] --> 4 bytes
t1[1] --> 4 bytes
Then you assign a Test reference array to point to the memory locations. We actually now have two array elements that point to the first 2 bytes of each memory location:
t[0] --> 2 bytes (first 2 of t1[0])
t[1] --> 2 bytes (first 2 of t1[1])
Now when we ask the compiler to use the reference t to give us the original t1 items we can do it because they are in memory anyway.
This might be hard to understand without good pictures but this ascii interface doesn't allow that very well. The point here is to remember that variables in java are actually memory references that point to various locations and depending what is in the location tells the compiler what can be taken out. Also keep in mind that as classes are subclasses the memory locations get bigger.
Regards,
Manfred.
 
Hussain
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good work Rekha raoo
------------------
Muhammad Hussain
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Rao
This is explained in JLS 5.5

Test t[] = {new Test1(), new Test1()};
Test1 t1[] = (Test1[])t

The declared type of t is Test[], the runtime type of t is the same. You can check it via System.out.println(t);
JLS 5.5 says that a compile error will come up if the declared type of t is not related to the one inside the cast operator. Here there is no such error because Test is the father of Test1.
JLS 5.5 says that the JVM will produces a ClassCastException unless the runtime type of t is not assignable compatible with the type within the cast operator. JLS 5.2 tell us that for type X being assignable compatible to type Y, both must be the same type or X must be a subtype of Y. So there you got the ClassCastException because Test is a superclass of Test1 is not assignable compatible with Test1.

Test t[];
Test1 t1[] = {new Test1(), new Test1()};
t = t1;
Test1 t11[] = (Test1[])t;

Here no problem at all.
t has a declared type of Test[] and a runtime type of Test1[] because now is holding the object pointed by t1.
The compiler does not complain because Test is related to Test1. The JVM doesn't produce ClassCastException because Test1 is the same as Test1.
hope helps
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rekha,
i thought i am clear about this parent & child casting fundamental but ur example prooved me wrong!
after i read the answers i got cleared. it's same as working with single objects but i messed up as u have arrays
good one.
regards
maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic