Arne Deutsch
Arne Deutsch

Reputation: 14779

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

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 length)

instead of

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

?

Upvotes: 18

Views: 1439

Answers (2)

irreputable
irreputable

Reputation: 45443

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.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533660

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);

Upvotes: 21

Related Questions