possible duplicate:
how copy object in java?
i need clarification on differences between deep copy, shallow copy, , clone in java
unfortunately, "shallow copy", "deep copy" , "clone" rather ill-defined terms.
in java context, first need make distinction between "copying value" , "copying object".
int = 1; int b = a; // copying value int[] s = new int[]{42}; int[] t = s; // copying value (the object reference array above) stringbuffer sb = new stringbuffer("hi mom"); // copying object. stringbuffer sb2 = new stringbuffer(sb); in short, assignment of reference variable type reference type "copying value" value object reference. copy object, needs use new, either explicitly or under hood.
now "shallow" versus "deep" copying of objects. shallow copying means copying 1 level of object, while deep copying means copying more 1 level. problem in deciding mean level. consider this:
public class example { public int foo; public int[] bar; public example() { }; public example(int foo, int[] bar) { this.foo = foo; this.bar = bar; }; } example eg1 = new example(1, new int[]{1, 2}); example eg2 = ... the normal interpretation "shallow" copy of eg1 new example object foo equals 1 , bar field refers same array in original; e.g.
example eg2 = new example(eg1.foo, eg1.bar); the normal interpretation of "deep" copy of eg1 new example object foo equals 1 , bar field refers copy of original array; e.g.
example eg2 = new example(eg1.foo, arrays.copy(eg1.bar)); (people coming c / c++ background might reference assignment produces shallow copy. however, that's not mean shallow copying in java context ...)
two more questions / areas of uncertainty exist:
how deep deep? stop @ 2 levels? 3 levels? mean whole graph of connected objects?
what encapsulated data types; e.g. string? string not 1 object. in fact, "object" scalar fields, , reference array of characters. however, array of characters hidden api. so, when talk copying string, make sense call "shallow" copy or "deep" copy? or should call copy?
finally, clone. clone method exists on classes (and arrays) thought produce copy of target object. however:
the specification of method deliberately not whether shallow or deep copy (assuming meaningful distinction).
in fact, specification not state clone produces new object.
here's the javadoc says:
"creates , returns copy of object. precise meaning of "copy" may depend on class of object. general intent that, object x, expression
x.clone() != xtrue, , expressionx.clone().getclass() == x.getclass()true, these not absolute requirements. while typically casex.clone().equals(x)true, not absolute requirement."
note, saying @ 1 extreme clone might target object, , @ other extreme clone might not equal original. , assumes clone supported.
in short, clone potentially means different every java class.
some people argue (as @supercat in comments) java clone() method broken. think correct conclusion concept of clone broken in context of oo. afaik, impossible develop unified model of cloning consistent , usable across object types.
Comments
Post a Comment