File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCast Exception.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCast Exception.." Watch "ClassCast Exception.." New topic

ClassCast Exception..

Rekha Rao

Joined: Jan 23, 2001
Posts: 15
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{}
<some method>....
Test t[] = {new Test1(), new Test1()};
//ClassCastException, here:
Test1 t1[] = (Test1[])t;
<some method>....
Test t[];
Test1 t1[] = {new Test1(), new Test1()};
t = t1;
Test1 t11[] = (Test1[])t;
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
good work Rekha raoo
Muhammad Hussain

Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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

SCJP2. Please Indent your code using UBB Code
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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.
I agree. Here's the link:
subject: ClassCast Exception..
jQuery in Action, 3rd edition