Search code examples
javaarrayscloneoverriding

Where is clone() overridden for arrays in java?


An overridden clone() method of java.lang.Object class is available to all arrays.

Where is this method overridden for arrays?


Solution

  • It is defined by the JLS 10.7:

    The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
    A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

    Where and how it is overriden is implementation specific. For HotSpot for example, you can find it here at line 539 and following. If you drill down you will see that it calls a platform dependent method. For Windows-x86, for example, the pd_conjoint_jlongs_atomic method is located here line 97 and uses assembly code.