java_geek
java_geek

Reputation: 18035

How is Object[] cloneable

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

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

Upvotes: 4

Views: 929

Answers (2)

Thilo
Thilo

Reputation: 262534

All arrays in Java are Cloneable and Serializable.

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

Upvotes: 10

Jon Skeet
Jon Skeet

Reputation: 1500675

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.

Upvotes: 4

Related Questions