Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Stack implementation and shallow copy

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Stack implementation and shallow copy

    Im new the the shallow copy/deep copy concept. I need a little help understanding whats actually going on in this implementation:

    public class Stack1 {
     
    private Object value;
    private Stack1 rest;
    private boolean empty;
    public boolean isEmpty() { return empty; }
     
    public Stack1() { 
    value = null;
    rest = null;
    empty = true;
    }
     
    private Stack1(Stack1 other){ //Make fields of this object point to the same objects in another stack's fields i.e. shallow copy
    this.value = other.value;
    this.rest = other.rest;
    this.empty = other.empty;
    }
     
    public void push(Object x){
    this.rest = new Stack1(this); //What's going on here?
    this.value = x;
    this.empty = false; 
    }

    In the push method we make a shallow copy on the RHS which means that there is some object whose fields are referencing the same memory blocks as our current stack object (this). But we then go ahead and say, ok now take our current object (this) and set its rest to be this copy. But this.rest has just been altered, doesn't that mean the object copy's rest will be referencing itself? so its like some kind of infinite loop now???

    HELP!

    Thanks


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Stack implementation and shallow copy

    Am I thinking about this wrong?... is it only if I alter the object its referencing (using mutator methods) rather than the variable holding the reference? I think this might be my issue.. so if I did something like

    this.rest.pop()

    then a clone would also have its rest popped?

    but if i went this.rest = new Object()

    then the clones rest would still be the old this.rest?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Stack implementation and shallow copy

    It looks perfectly fine to me.

    A good way to treat variables in Java is as a pointer or address.

    this.rest = new Stack1(this);

    Before executing this line, this.rest points to something (could be an object or null, doesn't matter). An analogy would be if you had an address to a house on a piece of paper (say your friend Bob's house).

    In the constructor of the new Stack1 object, it's copying that address but not the object. This could be viewed as your friend Gary copying the address you have written down for Bob. Now if you both go to the same address, you'll find the same house and if you decide to paint the walls yellow, you're friend will see that Bob has yellow walls.

    However, back in the push method, you modify your this.rest. This is analogous to you changing the address you have written down to the address of Gary's house. It has no effect on the address your friend Gary has written down, which still points to Bob's house. Now if you go and modify Gary's house, the changes won't be visible at Bob's house.

    It is interesting to note, that by going to Gary's house you can still find Bob's house. Since Gary has Bob's address written down and you know where Gary lives, you just go to Gary's house and ask him. The java code would be this.rest.rest (assuming you're at the end of the push method).
    Last edited by helloworld922; March 20th, 2012 at 12:36 AM.

  4. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    Shaybay92 (March 20th, 2012), Tjstretch (March 20th, 2012)

Similar Threads

  1. I need help for this copy byte code ...
    By ricaclaire16 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2012, 07:00 AM
  2. Stack implementation with given condition
    By Magesh in forum Member Introductions
    Replies: 2
    Last Post: August 31st, 2011, 01:17 AM
  3. Implementation Stack Using Array
    By rainbow9 in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 20th, 2011, 09:48 AM
  4. Deep copy of ArrayList?
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2011, 01:30 PM
  5. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:43 PM