Why is System.arraycopy native in Java?

James B picture James B · May 5, 2010 · Viewed 25.2k times · Source

I was surprised to see in the Java source that System.arraycopy is a native method.

Of course the reason is because it's faster. But what native tricks is the code able to employ that make it faster?

Why not just loop over the original array and copy each pointer to the new array - surely this isn't that slow and cumbersome?

Answer

Péter Török picture Péter Török · May 5, 2010

In native code, it can be done with a single memcpy / memmove, as opposed to n distinct copy operations. The difference in performance is substantial.