Author Topic:   ArrayStoreException
sree
ranch hand
posted March 17, 2000 12:00 PM             
Could somebody give me an example in which scenario this exception will be thrown.

Thanks,
sree.

Jim Yingst
sheriff
posted March 17, 2000 12:34 PM             
There's an example in the API for ArrayStoreException.

maha anna
bartender
posted March 17, 2000 12:45 PM             
Sree, here you go,
 
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
Class Test {
public static void main(String[] args) {
ColoredPoint[] cpa = new ColoredPoint[10];
Point[] pa = cpa;
pa[0] = new Point();

}
}


All object arrays can be converted to another array of class type of one of the classes in its inherritance hirearchy. The regular object conversion/casting for objects apply here also for arrays of objects . Here in the above example, ColoredPoint extends Point. So an array of 'ColouredPoint' can be assigned to another array of 'Point'.
The compiler wouldn't say anything since both arrays's types are compatible.

But for arrays, all elements must be of the same/assignable to the class type of the array. It is the rule. So here all elements of array cpa must be ColoredPoint/one of its subclasses if there is any.Simillarly an array of Point can have only 'Point' objects/any of its subclasses as its elements. When you assign an element of an object array, here also the regular conversion/casting rules apply.

Having said that, when we try to assign one of the element of 'pa' array (which is actually a ref to an array of ColouredPoint , NOT Point) to an object of class type 'Point', an ArrayStoreException will occur. Because the physical original array which was created first , is of type 'Colored Point' NOT Point. For regular object casting/conversion can we assign a var of type 'ColoredPoint' to an object of type Point ? ( ColoredPoint cp = new Point() ) No. This is the reason why this RuntimeException is thrown.

If it would have been like the foll. code then it is ok. Because a subclass object can be auto-converted to a var of type of its superclass without any problem.


Point[] pa = new Point[10];
pa[0] = new ColoredPoint();

Also note that for primitive arrays the rules are slightly different. An int[] can be converted to only another int[] array. No auto-conversion. (i.e) an int[] can't be assigned to a long[] array. I think you know these things well. .
regds
maha anna

[This message has been edited by maha anna (edited March 17, 2000).]

sree
ranch hand
posted March 17, 2000 12:55 PM             
Thank you very much Anna & Jim.

Anna i totally got it now.

sree.

sree
ranch hand
posted March 17, 2000 03:12 PM             
Hi,

I read this follwing statement in JLS. It's regarding arrays. It says "There is no permitted conversion from any array type to any class type other than Object or String".

I thought from array type to class Object is the only permitted conversion.

String s;
Object o;
int arr[] = new int[10];
s = arr; // is this legal??
o = arr; // this is o.k.

Thanks.

maha anna
bartender
posted March 17, 2000 05:49 PM             
Sree,
When I was reading JLS I also had the same doubt. But when I did some research on this, I came to a conclusion in fact this is a mistake. Not only here, there are some other obvious mistakes in other chapters also. I don't remember where. After your post I went and did some analysis in Chapter 5 Conversion, I found there are the points where your point is analyzed. You can see here JLS says at all points that an array can be converted to a class of type only Object except at only one sentence which you and I ( ) have the doubt . I came to a conclusion it must be an error.
May be Jim can add to this.
regds
maha anna

5.1.4 Widening Reference Conversions
------------------------------------
The following conversions are called the widening reference conversions:
- From any array type to type Object.

5.1.5 Narrowing Reference Conversions
-------------------------------------
The following conversions are called the narrowing reference conversions:
- From type Object to any array type.

5.1.7 Forbidden Conversions
------------------------------
- There is no permitted conversion from class type S to any array type if S
is not Object.
- There is no permitted conversion from any array type to any class type other than Object or String.

An array can be assigned only to a variable of a compatible array type, or to a variable of type Object.

The detailed rules for compile-time correctness checking of a casting conversion of a
value of compile-time reference type S (source) to a compile-time reference type T
(target) are as follows:
If S is a class type:
-If T is an array type, then S must be the class Object, or a compile-time
error occurs.

If S is an array type SC[], that is, an array of components of type SC:
If T is a class type, then if T is not Object, then a compile-time error
occurs (because Object is the only class type to which arrays can be assigned).

Jim Yingst
sheriff
posted March 18, 2000 12:36 PM             
Ah, there are a few more sections you missed:

quote:
5.1.6 String Conversions

There is a string conversion to type String from every other type, including the null type.


This is in addition to the widening and narrowing reference conversions listed in 4.1.4 and 4.1.5, and it explains the exception given in 4.1.7. Note that string conversion is not included under assignment conversion, so it's never done implicitly as the result of an = operator. It's only done here:

quote:
5.4 String Conversion

String conversion applies only to the operands of the binary + operator when one of the arguments is a String. In this single special case, the other argument to the + is converted to a String, and a new String which is the concatenation of the two strings is the result of the +. String conversion is specified in detail within the description of the string concatenation + operator (§15.17.1).


So the JLS is not in error here - it's just hard to understand, as usual.

mahaanna
unregistered
posted March 18, 2000 01:12 PM           
Thank you very much Jim. So now you got me? . I know the missed part ,as well as An array can be assigned to another ref of class type Object also. Then why did'nt I relate these two? If I would have related these 2, then I would have got the qstn of whether conversion can be done only through lhs=(type)rhs alone? Or is there any other means of doing it? Yes. I accept. Sometimes we got to think tooooo much. Anyway I am glad you came for rescue.
regds
maha anna

[This message has been edited by maha anna (edited March 19, 2000).]

|