Home > Back-end >  The small white, what is the copy of deep and superficial excuse me
The small white, what is the copy of deep and superficial excuse me

Time:12-07

Just learning Java on the second day, it's not a programming basis, encountered such a problem, is not very good, which can explain thoroughly the great god, feeling the information on the net the ambiguity, what is the case can abstract compare?

CodePudding user response:

Shallow test case is the only copy the reference, deep test case is to replicate a same new objects,

Such as a class

The class Person {public String name="Tom"; }

If a=new Person ();

B=a, b.n ame="Jim", the a.n ame becomes Jim, this is shallow test case,

If b=new Person (); B.n ame=a.n ame. The a.e quse (b) are equal, but b.n ame=Jim, a.n ame or Tom, this is deep test case,

CodePudding user response:

reference 1st floor icoolno1 response:
shallow test case is the only copy the reference, deep test case is to replicate a new object, the same

Such as a class

The class Person {public String name="Tom"; }

If a=new Person ();

B=a, b.n ame="Jim", the a.n ame becomes Jim, this is shallow test case,

If b=new Person (); B.n ame=a.n ame. The a.e quse (b) are equal, but b.n ame=Jim, a.n ame or Tom, this is the deep test case,

This assignment is not a constant pool, in the middle of the stack of hexadecimal memory address is the same, but is not the same as the content of heap

CodePudding user response:

refer to the second floor MuSa xiaoxiang reply:
Quote: refer to 1st floor icoolno1 response:
shallow test case is the only copy the reference, deep test case is to replicate a new object, the same

Such as a class

The class Person {public String name="Tom"; }

If a=new Person ();

B=a, b.n ame="Jim", the a.n ame becomes Jim, this is shallow test case,

If b=new Person (); B.n ame=a.n ame. The a.e quse (b) are equal, but b.n ame=Jim, a.n ame or Tom, this is the deep test case,

Such assignment is not a constant pool, in the midst of stack hexadecimal memory address is the same, but is not the same as the content of heap ah


With constant ok, Java inside is primarily a reference type, different arrays can refer to the same element, an array sort query, do not affect don't an array, but the element attribute assignment will influence, after deep copy, although the content is the same, but is the new object element, is to establish a true copy of
  • Related