aspose 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

gat devi
Greenhorn

Joined: Nov 03, 2000
Posts: 1
Hai,
Could anyone help me regarding this code which I had got thru the forum.
class Testc{
public static void main(String args[]){
A[] a,a1;
B[] b;
a = new A[10];
a1=a;
b = new B[20];
a=b;
b= (B[])a;
b= (B[])a1;
}
}
class A{}
class B extends A{}
I need a clear explanation of this application. Could anyone help me please.
regards
Gat

lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
A nice discussion is here at
http://www.javaranch.com/ubb/Forum24/HTML/005268.html
on the very same question.
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
class A{}
class B extends A{}
lets have look at clause in JLS
"A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array."

int sr[];//no memeory
int sr=new int[]//object created

if u write
A arr[]={new ??,new??};
irrespective of type of member the array object will be type A[]
i.e if u write
A arr[]={new B(),new B()};
or
A arr[]={new B(),new A()};

ur array object is type of A[]
now whwn ever u try to cast it to subclass

B ar[]=(B[])arr;
there will be no error at compile time coz what compiler want is that classes of array should be in class hiearchy.
at runtime there will be exception thrown coz array type of arr is A[] and u r casting to B[].it must be B[] or its subclass
Now if u write
B ar[]=new B[3];
A arr[]=(A[])ar;

There will be no exception as array object is of B[] type and which is subclass of A so Object of sublcass is also Object of Super class;
now come the real point how to do it???
see
A arr[]=new B[2];
now object of array Type is B[].
but u cannot have now like
arr[0]=new A(); //line 3
There will be ArrayStoreException will be thrown.
coz array is now of type B[] and that cannot hold Superclass object.however it can always have object of
subclass.
i.e
arr[0]=new C();

Following Lines from JLS chapter 10 u can see effect in line 3
"An assignment to an element of an array whose type is T[], where T is a reference type, is checked at run-time to ensure that the value assigned can be assigned to the actual element type of the array, where the actual element type may be any reference type that is assignable to T. "
At compile time, an assignment to an element of A is checked to make sure that the value assigned is a A. But since arr holds a reference to an array of B, the assignment is valid only if the type of the value assigned at run-time is, more specifically, a B.
Now apply same rule on ur problem

A[] a,a1; //line 1 No memory
B[] b;//line 2 No memory
a = new A[10]; //Array object of type A[]

a1=a;//line 4Array Object of type A[] is assign to A[] a1

b = new B[20];//Array object of type B[]

a=b;// line 6 B[] is assign to A[]

b= (B[])a;// line 7 B[] is assign to B[]

b= (B[])a1//line 8 Exception thrown

Main line is line 6 in which B[] is assign to a which is supose to take array type of A[] since B[] is also
A[] this is valid
But Now a have array type B[]
and if u try to write this a[0]=new A();
u will get ArrayStoreException coz object of a is B[] type it can only hold references to object of class B or its subclasses
line 7 is valid due to line 6 if u commented line 6 u will get exception at line 7 due to line 6
a now has B[] object and u r casting it to B[] no Problem at all (Thats answer for Gibson question)

Now at line 8 u get exception coz runtime object of a1 is A[] and u r casting it to B[] thats why exception is thrown

if u place line 4 after line 6 u will not get error i hope by now u all know why.
Hope this will Help

don't forget to see http://www.javaranch.com/ubb/Forum24/HTML/005116.html

[This message has been edited by yogesh sood (edited November 03, 2000).]


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException