Shadow copy vs deep copy

hi,
any body tell me what is the difference between shadow copy and deep copy of a object.

would you tell me any prectical example of this. i
mean when i should use shallow copy and when i should
use deep copy of object.I'm hoping someone else will step in here, because in my OO thinking, there's seldom a need for copying an object at all.
One practical example, though: Say you're copying a list of objects. After the copy, you want to alter the objects in the copied list, but you don't want the objects in the original list altered. In this case, you need a deep copy. Had you only done a shallow copy, both lists would have referred to the same objects, and therefore, any change in an object would be visible through both lists.
clone() method makes shallow copy or deep copy.You'd have to consult the doc for the class you are cloning. For example, the doc for LinkedList.clone() says "Returns a shallow copy of this LinkedList."

Similar Messages

  • Shallow copying and Deep copying ???

    hi,
    what is meant by shallow copying and deep copying in context to clone() method found in Object class?
    thanks,
    swapnaja

    swapnaja wrote:
    Shallow copying: only cloneable object is copied but any object references within that object would not be copied but would rather be shared.
    Deep copying: cloneable object along with any object references contained within that object will also be copied.
    Am i right?Yep; when you want to make a deep copy you have to watch out for circular references, i.e. if you have made a copy of an object already you shouldn't make another one during the deep copying process; (see how the ObjectOutputStream does things).
    kind regards,
    Jos

  • Copy objects ('deep copy')

    Could you help me please, I'm trying to perform a copy between object A and object B they are different instances of the same object. Object A contains other objects inside it and goes like this into a level of 4 or 5 object inside others. The purpose is to copy the attributes from objects B which have a value of null in the object A so I have to go thru all the objects and compare each attribute. I thougth about some solutions but they are to complex and difficult to implement, could you give some ideas...
    PF

    Implement a copy method in each of your objects, that
    1. Copies all simple datatypes and objects directly to an object of the same type
    2. Invokes your copy method on all non-simple object attributes
    Your copy method should have a consistent method signature of the form..
    public void myCopy(thisObject obj);This is not an overly complicated solution.

  • Creating a deep copy

    I wrote a stack implementation using a LinkedList, I have null constructor to create the first stack and it works fine but now i have to create a copy for a second stack using the element of the first constructor
    this my code
    import java.util.LinkedList;
    public class Stack<T>{
      private LinkedList<T> stack;
      public Stack(){
        stack =  new LinkedList<T>();
      public Stack(Stack<T> s) {// the problem is here it compile but when i run
                                                     // the program the second stack raised an exception because is empty
                                                     // i want to create a deep copy with the elements of the first stack
         stack = new LinkedList<T>();
      public boolean isEmpty(){
      return stack.isEmpty();
      public void push(T item){
       /*method to add object information
        *pre the object is not full
        * pos: the object is inserted
          stack.addFirst(item);
      public T top() throws EmptyStackException{
        if (!stack.isEmpty()){ 
          return stack.getFirst();
        else{
          throw new EmptyStackException("StackException on top " + " Stack empty");
      public T pop() throws EmptyStackException{
        /*method to remove an element of the stack
        *pre the object is not empty
        * pos: the object is remove
        * response throws an exception
        if (!stack.isEmpty()){ 
          return stack.removeFirst();
        else{
          throw new EmptyStackException("StackException on top" + "stack empty");
    public int numElements(){
        return stack.size();
    public void clear(){
      stack.clear();
    public class EmptyStackException extends Exception{
            public EmptyStackException( String message){
              super(message);
    }

    wel well... are you doing this on a mobile phone???
    neways to get to ur question u can use the clone() method to generate a clone of your object which is the same as a deep copy for eg:
    public class CloneTest{
        CloneTest test1 = new CloneTest();
        CloneTest test2;
        CloneTest(){
          //Some initialization code;
        CloneTest(CloneTest temp){
             test2 = test1.clone();
    }clone is a method of the object class.

  • Help with a deep copy

    What is the best practice for sending doing a deep copy between two View Controllers in an iPhone app. I've designed an app that sends over arrays of arrays and the deeper items aren't coming over as easily as I thought. Do you explicitly copy? Just alloc placeholders and set them equal? Links to any samples would be greatly appreciated.
    Is it a best practice to avoid that kind of data passing - as being overly complex?
    Background:
    I'm building an iPhone application where I need to send a complex object from one view controller to another - This complex object is an array of arrays of objects.
    I can't just copy a reference to the complex object over to a variable on the receiving view controller, as none of the nested array contents come over.
    So I think I need to do a deep copy - but need advice on best practices for that. The array of arrays contain objects, so ideally, Id just be able to :
    Thanks for any advice!

    There are many ways of solving this.
    One way is to centralize the information:
    You create a singleton class that is the datasource for both view, so you don't need to pass in anything. If you can't do that because the datasources are generally different except for this specific array of arrays, then make the singleton class be the source of that array and nothing else.

  • Clarification regarding deep cloning/deep copy

    Hi,
    While performing deep copy of an object should all the subobject that the parent object constitute also constitute the clone method.
    For Eg:
    I have a class called Document and a class called Field. Document constitutes of List<Field> fields. Field class has a String fieldName as its member variable.
    I need to perform a deep copy of Document object. Should Field class also implement the clone method or can I create field objects in the documents clone method and fill these fields with values from the existing field list to form a new Document with these new fields.
    Or do I call clone() method on each field to get a new field object and
    add it to the new Document object that I am creating in the clone() method .
    //First approach
    public Document clone(){
    Document doc = new Document();
    List<Field> fields = doc.getFields()
    Field newField = null;
    for(Field f: fields){
        newField = new Field();
        newField.setName(f.getName());
       doc.addField(newField);
    return doc;
    //Second Approach
    public Document clone(){
    Document doc = new Document();
    List<Field> fields = doc.getFields()
    Field newField = null;
    for(Field f: fields){
        newField = f.clone()
       doc.addField(newField);
    return doc;
    }Please let me know which is a better approach and the standard coding
    practise.
    Thanks

    the second method is better, because it lets you the possibility to create a subclass of Field with a specific clone method, integrate some instances in a Document object; without changing the Document.clone method.

  • Deep Copy

    Hi all,
    I have a question I can't find any documentation about, not on google, not here on the forums. We are developping a EJB J2EE application (with glassfish) with flex as the view. We are using BlazeDS to interchange objects back and forth between flex and java.
    The problem we are having is we have to do a deep copy of an object tree, but blaze by default copies shallow.
    The setup is like this:
    We generated the actionscript classes that represent the java classes on the client side with a code generator named Gas3 (the only one out there, no?). This generator created classes with RemoteClass and Bindable annotations already in place. Gas3 also implemented the IExternalizable interface but we threw that out because it gave us problems in java. We have our EJB services configured in the remoting-config.xml file and everything works except for deep copy so I don't think there is anything missing, no?
    Is somebody aware of a way we can achieve a deep copy? I imagine there would be an xml tag or attribute that would allow for this ...
    Any help would be greatly appreciated!
    Thank you,
    Bruno Windels
    3S

    Or is it creating a deep copy of each element in the Array and placing them into a new array object?

  • How to deep copy a component configuration?

    Hi Experts,
    I want to deep copy a component configuration named EHHSS_RAS_OIF.
    But following was the error shown.
    Can you please help/guide to do the same.
    Thanks in advance.
    Sanket.

    Hi Sanket,
    I think you might be copying the config in Unit test client but not in Development client.
    Thanks
    KH

  • Deep copy of String

    As String is called by reference, how can I perform deep copy?
    Anything similar to System.arraycopy for String?

    That's funny, this works for me:
    String k = "kkkkkkkk";
    String m = k;
    Now, when I change 'k' (f.i. k = k.substring(0, 2); )
    'm' does not change.
    So that's a deep copy!
    ;JOOP!No - it's not a deep copy. Your statement k = k.substring(0, 2) does not change the string that was referenced by k previously, but changes the k variable itself so that it references a new string.
    It's really important to get a handle on the difference between objects
    on the one hand, and references to them on the other. Without that understanding you have little hope of writing correct and efficient Java programs.
    Sylvia.

  • How does deep copy apply to arrays?

    I have a service object that has a method that returns an array of
    objects. The return type for the method is defined with the copy option.
    I found documentation that states that the copy option creates a deep
    copy of the return variable on the partition that called the method.
    My question is: If the return type for the method is an array of
    objects, will the copy option create copies of all objects/elements in
    the array?
    We wish to eliminate any object references to the service object's
    partition. Any insight would be greatly appreciated.
    Thanks in advance,
    Van Vuong
    Phone: 972.985.5289
    Pager: 972.320.2232
    VoiceNow Pager: 972.330.0822
    E-mail: [email protected]
    PAGE NET

    Copy option always copies deep. Remember, also if you pass the array
    accross partitions, whether you specify copy or not, it is going to copy
    and copy deep.
    In an array, I am not sure if have the problem, because unless the array
    in-turn holds a huge tree, the array object may be wide, but not deep.
    Some thing to think about??
    Venkat

  • ObjectUtil doesnt deep copy correctly

    I am using an ObjectProxy for a complex object containing
    arrays of objects. I bind a datagrid to the array and . notation to
    fields. This all works fine, datagrid behaiving properly, inserts
    and deletes working properly.
    I want to have an undo function that will put the objectproxy
    object back to its original state. So when i open the screen with
    the datagrid I do a ObjectUtil.copy() and use this latter if the
    user wants to revert changes. The problem is that the copy doesn't
    do a deep copy. In the Object is an ArrayCollection of userdefined
    objects. These objects instead of being copied as their type are
    being copied as plain objects.
    The serialize meathod the copy uses doesn't retain the types
    of user defined objects.
    Will I have to create clone or copy methods for every object
    type I have?
    If so do I just need to make them ezxternalizable? How will
    the objectutil copy pick up these methods?
    Thanks,
    Jeff

    Unfortunatlly you will need to implement it for every class
    that has complex objects, as it does not know how to deal with
    them. Your classes need to extend EventDispatcher and/or implement
    IExternalizable.
    Then create methods writeExternal and readExternal for every
    local variables you need to retain. eg..
    public function writeExternal(output:IDataOutput):void {
    output.writeUTF(msgString);
    output.writeObject(anyObject);
    output.writeInt(id);
    public function readExternal(input:IDataInput):void {
    msgString = input.readUTF()as String;
    anyObject = input.readObject()as Array;
    id = input.readInt() as int;
    }

  • Deep copy a PriorityQueue

    Dear all,
    I am struggle with deep copying a priorityQueue. Anybody has a clue about his. It seems onyl swallow copy is allowed.
    Cheers,
    Ke

    See
    http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html
    http://www.google.com/search?num=100&hl=en&c2coff=1&q=java+deep+shallow+copy&btnG=Search

  • Deep copy cloning

    hi guys, i have an object which extends defaultmutabletreenode, basically it is a leaf on a tree and these leafes (not leaves then i guess but you know what i mean) have leaves themselves, as shown below, every node has an arraylist and these nodes also contain arraylists. i want to clone this object to see if it is quicker than re-creating it all of the time but i am a little hesitent as i am worried about getting a deep clone to work and unfortunately i couldnt find one that someone has already written.
    am i right in thinking that you cant simply clone mychildren, you then have to go to every chidl and clone its children also??
    below is the top of the class so you cans ee what it looks like...could anyone post a loop that would clone the tree or if it is a lot of trouble just a description of what i need to do would be great!
    public class Node extends DefaultMutableTreeNode
    private RootNode myRootNode;
    private ArrayList<Node> myChildren;
    thanks in advance to all who reply
    Danny =)

    Yeah in that article I'm pretty sure he talks about putting it through a PipedInputStream to a PipedOutputStream (or vice versa?). That should be considerably faster than writing to a disk.
    I think you're going to be better off writing your own deep copy method though, it's a pretty simple structure, not worth the extra overhead...
    p.s. Are you positive that you need to clone everything all the time?

  • Copy isn't copying everything?

    Hey ya'll                                                                                             Level: Newbie'ish OS: Win 7 64bit  Ps: Cs6
    So, i went back to an image i made a while ago because i wanted to remember how i went about doing something. i also got out my notebook where i kept track of particular aspects of the lesson.
    sadly, i do not seem to have written down the one thing i am looking to recall.
    Here is a pic
    see the open path that defines the arm inside the red circle?
    well, i'm working on a female version of this but i can't seem to recall how i went about duplicating the path so that the stroke and layer styles are copied as well.
    This is how i made it ...
    i selected the pen tool > set to path > drew out my path > right clicked to select stroke path
    then i double clicked on the layer to add a gradient overlay and a drop shadow
    now, when i made the guy above all i wrote down to do is to copy and transform but now when i do this only the path is copied.
    i have tried copy, copy merged, paste, paste special and paste in place.
    when i use ctrl + J and then flip it the stroke and ls's aren't copied. ??
    ctrl + j makes a new layer with the layer styles listed but when i did it the first time this is what my layers look(ed) like
    see how the "right arm" layer doesn't list the layer styles?
    can anyone Please remind me how one goes about copying and pasting an open path such as this so that one has a layer without the styles listed?
    i have also used alt + ctrl + t and dragged to copy but this also makes a layer with the styles listed.
    i know, i know ... this may seen Crazy to want to know but i do just really want to recall how i ended up with a copy on a layer without the styles listed.
    thank you so much in advance for your time & efforts. you're much appreciated.
    yup, i've been all through the FAQ's and when i got to the manual i think maybe i'm just not using the right key words to find the answer.

    Hi conroy ...
    thank you for reading my post. that works great for Copying without the Layer Styles
    however, that wasn't quite what i was looking for and so i think i did not ask my question the right way.
    while going through the Layers of my original image i noticed that the opposite side of the open Path that defines the arm did not have any Layer Styles listed on its layer and i could not recall how i went about creating the opposite side so that the Layer Styles are Copied but the Layer of the Copy did not list the Layer Styles.... and so that the Styles are also Flipped/Transformed accordingly.
    i would use Ctrl + J to duplicate the layer but when i would go to Transform/Flip it Horizontally this is what would (and does) happen ... it appears that only the Path is Duplicated and Transformed... (this was drawn with the Pen tool set to Path)
    but actually when the Move tool is selected you can pull out the Copy made by Ctrl + J ... like this ...
    but what to do with that pesty Path with the Handles?? to get rid of this go to the Paths palette (if you don't have it open over by your Layers palette just go to Windows > Path) and click the Trash Can icon at the bottom of the Paths palette.
    Now, once you've removed the Path with the Layer of the Copy selected in the Layers palette go to Edit > Transform > Flip Horizontal
    but this leaves you with an image where the Layer Styles are not Flipped accordingly like so ...
    so, what to do? and this is what i was trying to recall what i had done with my original image and why the Copied Layer did not have the Layer Styles listed
    in order to Duplicate/Copy a Path or Shape so that the Layer Styles are also Transformed accordingly
    1. Have the Layer selected in your Layers palette that you've drawn on
    2. Go to your Paths palette and click the Delete/Trash can icon (you're deleting the Work Path)
    3. Press Ctrl + J
    4. Click the New Layer icon to make a new layer above the Copy
    5. Shift click to select both the Copy and the Blank New Layer > Go to the Drop down arrow to the right where your Layers & Path palettes are and choose > Merge Layers OR hit Ctrl + E (this flattens the artwork so that the Layer Styles will be Transformed accordingly)
    6. Go to Edit > Transform > Flip Horizontal
    see how the Drop Shadows are now the same?
    here's what the Layer panel looks like for the image above...
    well, there ya have it. my bad ... i think i asked my question the wrong way.
    i didn't want to leave anyone hanging and also i really needed to figure it out. hope this helps a "newbie" like me
    conroy ... i REALLY APPRECIATE your taking a whack at trying to decipher what i was trying to ask
    take care everyone
    ps. the reason i said ... "copy isn't copying everything" is b/c it seemed like the stroke wasn't being copied when i flipped it and only the path outline w/the handles was visible ... i did not realize the copy was underneath the original

  • Difference between Original copy and free copy of application server 10g

    what are the difference between Original copy and free copy of application server 10g

    Hi,
    I think that there aren't free copy of Oracle AS.
    If you are a developer or a system architect, you may download Oracle AS for your test.
    Mauro

Maybe you are looking for

  • Photoshop CC2014 drawing lag issues...

    Every since the big updates of 2014 (CC, Yosemite), and the ensuing debacle of hardware/software issues, I finally have things working again.  Except for one little caveat: Everytime I restart Photoshop (every morning for the most part), if I don't g

  • Hdv with slow motion does flicker

    I´m editing now some clips at 1080 i and I need to use some slow motion clips. I use speed at 60 % and also active frame blendding and all it´s ok and I haven´t any trouble with the video. But when I do render I can see a big flicker at the picture.

  • Eceyones help kona or decklink

    Hey everone I'm looking to get a SD HD card but I do mostly dv I need the card for color, the only HD I do is P2 stuff so never caputre it through a deck, but I do need to have it down convert to SD monitor. Or could I just get way with the decklink

  • Groupwise 7 Exchange Gateway

    I have and exchange system with 130K users and GW system with 50K users connected together via Exchange Gateway 7 SP2. Is there a maximum in the number of contacts in either direction? What is the maximum number of Post Offices on GW side supported b

  • Canvio Slim 2 - 1 TB HDD

    Just recieved the HDD today and I can't get it to show up on my MBP 6,2 on OS X  10.6 or Windows 7 using Parallels. I tried to reformat it in Disk Management to use with Time Machine but it doesn't show up there. In control panels, under device manag