Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

arrays

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)

?

like image 273
Arne Deutsch Avatar asked Jun 09 '11 08:06

Arne Deutsch


People also ask

How does system Arraycopy work?

arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.

Does System Arraycopy create a new array?

arraycopy() simply copies values from the source array to the destination, Arrays. copyOf() also creates new array. If necessary, it will truncate or pad the content.

Is system Arraycopy deep copy?

System. arraycopy does shallow copy, which means it copies Object references when applied to non primitive arrays. Therefore after System.

Which of the following can be used to copy data from one array to another system clone system Arraycopy system Copyarray none of these?

Answer: There are different methods to copy an array. You can use a for loop and copy elements of one to another one by one. Use the clone method to clone an array. Use arraycopy() method of System class.


1 Answers

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);
like image 177
Peter Lawrey Avatar answered Oct 05 '22 18:10

Peter Lawrey