Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Can someone explain the clone interface in noobish terms?

  1. #1
    6thDAY is offline Member
    Join Date
    Aug 2010
    Rep Power

    Default Can someone explain the clone interface in noobish terms?

    I searched online about how cloning works but I only understand half of what's going on when cloning occurs.

    From my understanding, when you clone an object1 to an object2 you're cloning the reference to the memory address of clone1, and not the actual object itself??

    So pretty much object1 and object2 refer to the same memory address?

    Does that mean if u update object1, object2 will receive the same update?

    I read that although they have the same reference they are not the same object?? I am confused by that....

    I apologize for the questions I just need a plain English translation of what's happening

  2. #2
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Rep Power

    Default Re: Can someone explain the clone interface in noobish terms?

    clone() (if supported) copies the object to the new memory location, vs. the [bold]MyObject a = b[/bold] which makes a and b point to the same object i.e. reference the same object.

    Java by default is by reference. There are a few exceptions to this rule, most notably that primitives create a new primitive in memory i.e. [bold]int c = d[/bold] creates a new int c and assigns whatever value d has currently to c.

    The by reference nature of java can cause confusion if you're not ready!

    Look at this:
    Java Code:
    import java.util.LinkedList;
    public class Sandbox{
    	public static void main(String[] args){
    		LinkedList<String> ll = new LinkedList<String>();
    		LinkedList newll = ll;
    		System.out.println(ll); //Yep!  This is why you clone()
    		newll = (LinkedList)ll.clone(); 
    		System.out.println(ll); 		//Much better
    [A, B]
    [A, B, C]
    [A, B, C]
    [A, B, C, D]
    (OK, that's not a quote; but rather output)

    Creating a clone protects your initial object!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Can someone explain the clone interface in noobish terms?

    Quote Originally Posted by SJF View Post
    Java by default is by reference.

    Java is pass-by-value. Primitives are passed by value. References are passed by value. Objects/instances are not passed at all.

    Passing a reference by value means that the value of the passed parameter is copied to a new variable, just the same as when primitives are passed by value. Since the new variable has the same value, it refers to the same object.

    Repeat after me: Java has no mechanism to pass-by-reference.

    quad64bit likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

Similar Threads

  1. Why is clone() not in the Clonable Interface?
    By garyiskidding in forum Advanced Java
    Replies: 4
    Last Post: 05-29-2011, 04:27 AM
  2. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  3. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  4. A very, very, very noobish question
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:37 AM
  5. Plz explain "Interface"
    By mrblue in forum New To Java
    Replies: 4
    Last Post: 06-26-2009, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts