Array types confusion

The thread that was deleted addressed what's probably a common source of confusion. To that end, here are the highlights:

class P{}

class Cextends P{}

P[] arr =new C[1];

arr[0] =new P();// compiles, but ArrayStoreException at runtime

C extends P, but C[] doesnot extend P[]. Arrays extend Object.

[url http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.8]JLS 10.8 Class Objects for Arrays[/url].

It compiles because when doing the assignment into arr[0], the compiler only knows that arr is an array of P and that new P() produces a P. The compiler doesn't know that arr happens to point to a C[].

You get the exception because the array object you have is a C[], and you can't put a P into a C[] becase a P is not a C.

[1115 byte] By [jverda] at [2007-10-2 11:03:44]
# 1
What I said, it's basically about mixing up reference type and object type.
CeciNEstPasUnProgrammeura at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 2

> [url http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.8]JLS 10.8 Class Objects for Arrays[/url].

And if you scroll down a bit from there, you'll find almost exactly the same thing as what I was saying, in [url http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.10]10.10[/url]

jverda at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 3

*sigh*... when will they finally make up a computer language that's foolproof, so we can use this huge reservoir of nimb-headed people who'd work for a nickel and a piece of dried bread -- and be grateful -- and get rid of these arrogant pr|cks who demand to get paid well for just hacking some stupid words in their stupid "machines"... which can't be such a big deal anyway.

da.futta at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 4
...and don't get me started on your overuse of colons.
jverda at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 5
> The thread that was deleted ......Why was that?
ScarletPimpernela at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 6
AU infection
tsitha at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 7
> > The thread that was deleted ......> > Why was that?I assume because goldie started it and somebody ARed goldie.
jverda at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 8
> > The thread that was deleted ......> > Why was that?What's the most common cause of thread "disappearance" over the past year?
yawmarka at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 9
> *sigh*... when will they finally make up a computer language that's foolproof"Anything that can be used by monkeys will only be used by monkeys"kind regards,Jos
JosAHa at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 10
> What's the most common cause of thread> "disappearance" over the past year?I know this one! I know! People just cutting and pasting the word ********** a thousand times!
Laszlo.a at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 11

<cite>

P[] arr = new C[1];

arr[0] = new P(); // compiles, but ArrayStoreException at runtime

C extends P, but C[] does not extend P[]. Arrays extend Object.

</cite>

What do you exaclty mean by saying "C[] does not extend P[]" ?

There is no place to explicitly declare an inheritance relationship between the two array types. But this line's getting compiled

P[] arr = new C[1];

shows that P[] is assignable from C[].

Class.getSuperclass() returns Object.class for an array type indeed.

BIJ001a at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...
# 12
> What do you exaclty mean by saying "C[] does not> extend P[]" ?> ...> Class.getSuperclass() returns Object.class for an> array type indeed.That's what I mean.
jverda at 2007-7-13 3:36:36 > top of Java-index,Java Essentials,Java Programming...