Skip to content
Advertisement

What is more efficient: System.arraycopy or Arrays.copyOf?

The toArray method in ArrayList, Bloch uses both System.arraycopy and Arrays.copyOf to copy an array.

JavaScript

How can I compare these two copy methods and when should I use which?

Advertisement

Answer

The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arraycopy copies into an existing array.

Here is the source for Arrays.copyOf, as you can see it uses System.arraycopy internally to fill up the new array:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement