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)
?
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.
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.
System. arraycopy does shallow copy, which means it copies Object references when applied to non primitive arrays. Therefore after System.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With