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.
The function MemberwiseClone creates a new objects whose fields are bit-for-bit copies of those in the original structure. It is a necessary part of any inheritable class which allows cloning without use of Reflection or serialization, but it is only a small piece of the overall puzzle.
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed.
In C#, Clone() is a String method. It is used to clone the string object, which returns another copy of that data. In other words, it returns a reference to this instance of String. The return value will be only another view of the same data. Clone method called directly on current String instance.
Assuming the clone()
call in Java is just calling the Object.clone()
implementation, then I believe they have the same behaviour:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With