Code review request for 4881419 The type of X[].clone() should be X[]
Martin Buchholz
martinrb at google.com
Fri Sep 3 06:22:56 UTC 2010
On Thu, Sep 2, 2010 at 17:50, Joe Darcy <joe.darcy at oracle.com> wrote:
> Martin Buchholz wrote:
>
>> The usage T[] suggests that T is a reference type, as with generics, which
>> is not the case here.
>>
>> More precisely, both the runtime types and compiletime types of the object
>> returned from clone of any array is identical to its input.
>> Is there a non-confusing way of saying that?
>>
>
> How about
>
> "...and that the return type of the {@code clone} method of an array type
> {@code T[]} is {@code T[]} where T is either a reference or a primitive
> type."?
>
>
So I went and actually read the Object.clone spec, and it does indeed
guarantee that the (runtime) class of the returned value is the same as the
class of the argument, for arrays. As for the compile-time type, an
experiment demonstrates that the return type is indeed exactly the same,
including for non-reifiable array element types, e.g. this compiles:
Class<? extends Number>[] a
= (Class<? extends Number>[]) (new Class<?>[42]);
Class<? extends Number>[] clone = a.clone();
So Ulf's restriction to non-reifiable T seems not to be correct.
My suggested wording is:
"...and that the return type of the {@code clone} method of an array type
{@code T[]} is {@code T[]} where T is any reference or primitive type."
Martin "Objects have class; Variables have type"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20100902/b330ffaa/attachment.html>
More information about the core-libs-dev
mailing list