Caner
Caner

Reputation: 59238

.Net MemberwiseClone vs Java Clone

I'm converting C# code to Java. There are many different places that relies on .Net MemberwiseClone in the code I'm converting.

It seems that they both make shallow copy. So is it possible to simply replace these calls with Java's clone()? I want to make sure there are not any minor differences that would cause difficult to fix bugs.

Upvotes: 7

Views: 2209

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502286

Assuming the clone() call in Java is just calling the Object.clone() implementation, then I believe they have the same behaviour:

  • Another object of the same class is created
  • The fields are copied (up and down the inheritance hierarchy)
  • All copies are performed in a shallow way
  • No user-specified code is executed (constructors etc)

Upvotes: 6

Related Questions