From the documentation of Object#clone()
:
Note that all arrays are considered to implement the interface
Cloneable
. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
But the documentation of java.util.Arrays
doesn't indicate that Arrays implements Cloneable
.
How do arrays implement Cloneable
?
You are confusing java.util.Arrays
, a normal class that contains methods to work with arrays, and arrays themselves, which are a rather special construct in the Java language but are nonetheless objects with a synthetic class. This is this class that implements Cloneable
. It also derives directly from Object
. Look at the JLS page on arrays which is pretty clear on the subject.
Look for example at this code (taken from the aforementionned JLS):
class Test {
public static void main(String[] args) {
int[] ia = new int[3];
System.out.println(ia.getClass());
System.out.println(ia.getClass().getSuperclass());
}
}
This will print:
class [I
class java.lang.Object