Search code examples
javaarraysclone

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


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);

But it seems to me there is no reason to favor this over:

int[] dest = orig.clone();

They're both shallow copies anyway. Probably these folks just don't realize that clone exists. So is there any reason not to use clone?


Solution

  • No. If you're really microbenchmarking, then maybe, depending on what JVM you're running. But in actuality, no.