views:

107

answers:

2
Object[] o = new Object[]{};
System.out.println(o instanceof Cloneable);

This gives true as o/p. I could not understand why?

+4  A: 

Arrays support (shallow) cloning, basically.

From section 10.7 of the JLS:

The members of an array type are all of the following:

  • The public final field length, which contains the number of components of the array (length may be positive or zero).
  • 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[].
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

and

Every array implements the interfaces Cloneable and java.io.Serializable.

Jon Skeet
+10  A: 

All arrays in Java are Cloneable and Serializable.

A clone on an array just copies the array (shallow copy, not cloning the contents).

Thilo