Is there any reason to prefer System.arraycopy() over clone()?

后端 未结 5 1929
深忆病人
深忆病人 2020-12-04 18:23

When copying an entire array, I\'ve often seen people write:

int[] dest = new int[orig.length];
System.arraycopy(orig, 0, dest, 0, orig.length);
5条回答
  •  青春惊慌失措
    2020-12-04 18:42

    Just guessing here, but there might be a good reason to use System.arraycopy because different JVM's could conceivably implement them in a way that takes advantage of native abilities of the underlying system for a performance boost.

    For example, a JVM implementation could use a native library call like memcpy which could potentially take advantage of some memory controller tricks to perform the action in some incredibly fast and clever way. However, the Object.clone implementation might not be a good candidate for such optimization due to its virtual nature.

提交回复
热议问题