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 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" Watch "ClassCastException" New topic
Author

ClassCastException

Mark Beavis
Ranch Hand

Joined: Apr 07, 2005
Posts: 43
Hi All -

Can someone tell me why the last line of code in the main() method throws a ClassCastException - I cannot figure it out...



Thanks in advance...
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Mark Beavis
Ranch Hand

Joined: Apr 07, 2005
Posts: 43
then wouldn't the previous line cause an exception also:



This seems to compile fine...

I actually thought that both lines would compile, since B[] IS-AN A[]
Surekha Reddy
Greenhorn

Joined: Nov 11, 2005
Posts: 20
Hi,

If u comment the line a=b then b = (B[])a will also give ClassCastException.

After a=b statement a is also referring to B[] object. So it won't give any exception.

Correct me if i'm wrong.

Regards,
Surekha.
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
Hello Mark,

I don't really think that B[] IS-AN A[]. Of course, I am sure others would correct me if I am wrong (I am trying to take the certification too).

But the reason is, B[] and A[] are of type Array Objects, whose elements would contain A and B objects/references. So you could say something like

a[0] = (A) b[0]; //I am not sure if the casting is correct yet, but you get the picture right?

AND NOT

b = (B[]) a;

because, as per the code, B extends A

AND NOT

B[] extends A[].

Please correct me if I am wrong.

Thanks,
-Vijay
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

then wouldn't the previous line cause an exception also:

No, because the 'a' reference points to the B[20] array at that point.
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
I just found out that my post was wrong. Please excuse me if you had alread read it in the first place.
Thanks,
-Vijay
Patrick Williams
Ranch Hand

Joined: Apr 03, 2005
Posts: 213

then wouldn't the previous line cause an exception also:





Not when line 6 says



Patrick<br /> <br />SCJP/SCWCD/SCBCD/SCDJWS/SCEA<br /> <br />Currently working on:<br />SCJP 6
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
1.public class ClassCastTest {
2.public static void main(String args[]){
3.A[] a,a1;
4.B[] b;
5.a = new A[10];
6.a1 = a;
7.b = new B[20];
8.a = b;
9.b = (B[])a;
10.b = (B[])a1;
11.}
12. }
class A{}
class B extends A{}

hi mark, here�s the explanation

Line 10 gives a class cast exception. Now lets see how and why-

The theme of the question is �object reference concersion and casting�, While answering such questions keep in mind a basic thumb rule �

NewType=OldType ; //Thumb rule- for conversion to be legal, �OldType� should be a subclass of
//NewType

Line 6 is fine because both a1 and a are references of the same class.
Line 8 is fine because b (an array reference variable) is reference of class B which is a subclass of class A
So this is fine as per the above thumb rule

Lines 9 and 10 have explicit cast used. Another thing that one should remember is that whenever one sees an explicit cast, one must do the following two checks in mind(in fact the compiler checks like this)-

1)Comile time check- check whether the reference on either side (right/left) is a subclass of that on
another side. At lines 9 and 10, above this condition is satisifed get any compiler error Since you
are casting a reference of a superclass into a reference of a subclass, the explicit cast is
necessary
2)Run time check � here you need to determine the runtime classes of references on both sides
For line 9, class of �a� is B (due to line 8). So line 8 is just casting a reference of class B to a refernce �b� ( which is again an objec reference of class B). This in O.K
But at line 10, run time class of �a1� is class A, whereas runtime class of �b� is B. Thus irrespective of the cast, assignment on line 10 is something like this �
Baseclass=superclass,
i.e. the purpose of using a cast has gone in vain.
Hence we get a runtime exception

Hope you got it. CORRECT ME IF I AM WRONG ANYWHERE.


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Originally posted by Surekha Reddy:
Hi,

If u comment the line a=b then b = (B[])a will also give ClassCastException.

After a=b statement a is also referring to B[] object. So it won't give any exception.

Correct me if i'm wrong.

Regards,
Surekha.


Hi Surekha,

you are right..

krishna.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException