开发者

Why "System.arraycopy" uses "Object" instead of "Object[]"?

开发者 https://www.devze.com 2023-03-11 20:57 出处:网络
Just curious: Someone knows why the method System.arraycopy uses Object as type for src and dest? Would be perfectly possible to use Object[] instead?

Just curious:

Someone knows why the method System.arraycopy uses Object as type for src and dest? Would be perfectly possible to use Object[] instead?

Why define:

arraycopy(Object src, int srcPos, Object dest, int destPos, int len开发者_运维百科gth)

instead of

arraycopy(Object[] src, int srcPos, Object[] dest, int destPos, int length)

?


Primitive array types like boolean[] and double[] do not extend Object[] but they do extend Object

This method allows you to copy any type of array, so the type is Object.

int[] a =
int[] b =
System.arraycopy(a, 0, b, 0, a.length);


It would be more statically typed if it has a version for Object[] and each p[] where p is a primitive. Like the numerous overloading methods in Arrays class.

The author of System.arraycopy either was lazy, or didn't like clutter in API.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号