Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these statements about Object.clone and how deeply it copies is correct?

  • A.clone makes a deep copy, recursively cloning every object a field refers to.
  • B.clone makes a deep copy for arrays and a shallow copy for every other field.
  • C.clone copies primitive fields only, leaving reference fields null in the new object.
  • D.clone makes a shallow copy: field contents are assigned across, not themselves cloned.

Answer: D

The javadoc is explicit that the method performs a shallow copy: a new instance is created and each field is initialised with exactly the contents of the original's field, as if by assignment, and the contents of those fields are not themselves cloned. So both objects end up sharing the same nested objects.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.