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

  • clone() makes a distinct copy of the first array with its own reference.
  • System.arraycopy() uses JNI (Java Native Interface) to copy
    an array (or parts of it), so it is
    blazingly fast, as you can confirm
    here;
  • clone() creates a new array with the same characteristics as the old array, i.e., same size, same type, and same contents. Refer to here for some examples of clone in action;
  • manual copying is, well, manual copying. There isn’t much to say about this method, except that many people have found it to be the most performant.
  • arraynew = arrayold doesn’t copy the array; it just points arraynew to the memory address of arrayold or, in other words, you are simply assigning a reference to the old array.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)