Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayStoreException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArrayStoreException" Watch "ArrayStoreException" New topic
Author

ArrayStoreException

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Could somebody give me an example in which scenario this exception will be thrown.
Thanks,
sree.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There's an example in the API for ArrayStoreException.


"I'm not back." - Bill Harding, Twister
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sree, here you go,
<pre>
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();
}
}
</pre>
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.
<pre>
Point[] pa = new Point[10];
pa[0] = new ColoredPoint();
</pre>
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).]
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thank you very much Anna & Jim.
Anna i totally got it now.
sree.
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ah, there are a few more sections you missed:
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:
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.
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
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).]
Virtual Machin
Greenhorn

Joined: Feb 28, 2001
Posts: 5
Hi Maha
Can you pls explain me in more detail ?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
sree and Virtual Machin
Would you please read the Name Policy and re-register using names that comply with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
 
subject: ArrayStoreException