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,
|
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,
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.
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. . [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; 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 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
|
Jim Yingst sheriff |
posted March 18, 2000 12:36 PM
Ah, there are a few more sections you missed:
quote: 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: 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).]
|
| | |