9

as I understood, the method clone() gives us the ability to copy object (no refernce) in Java. But I also read, that the copy is shallow. So what the point? Which ability the clone() method gives me, that a simple assingment doesn't?

5 Answers 5

23

The difference is that you can modify the cloned object without modifying the original object.

Point p = new Point(1,2);
Point p2 = p.clone();
Point p3 = p;
p2.x = 5;
p3.y = 7;

The change on p3 does feed back to p, while the change on p2 does not.

Let's see how the situation is after the individual statements (assuming 1, 2, 5, 7 would be objects):

Point p = new Point(1,2);

            .-----.    .-----.
 p  ----->  |  x -+--> |  1  |
            |     |    '-----'
            |     |    .-----.
            |  y -+--> |  2  |
            '-----'    '-----'


Point p2 = p.clone();

            .-----.    .-----.    .-----.
 p  ----->  |  x -+--> |  1  | <--+- x  |  <----- p2
            |     |    '-----'    |     |
            |     |    .-----.    |     |
            |  y -+--> |  2  | <--+- y  |
            '-----'    '-----'    '-----'

Point p3 = p;

            .-----.    .-----.    .-----.
 p  ----->  |  x -+--> |  1  | <--+- x  |  <----- p2
            |     |    '-----'    |     |
            |     |    .-----.    |     |
 p3 ----->  |  y -+--> |  2  | <--+- y  |
            '-----'    '-----'    '-----'


p2.x = 5;

            .-----.    .-----.    .-----.    .-----.
 p  ----->  |  x -+--> |  1  |    |  x -+--> |  5  |
            |     |    '-----'    |     |    '-----'
            |     |    .-----.    |     |
 p3 ----->  |  y -+--> |  2  | <--+- y  |  <----- p2
            '-----'    '-----'    '-----'


p3.y = 7;

            .-----.    .-----.    .-----.    .-----.
 p  ----->  |  x -+--> |  1  |    |  x -+--> |  5  |
            |     |    '-----'    |     |    '-----'
            |     |    .-----.    |     |
 p3 ----->  |  y  |    |  2  | <--+- y  |  <----- p2
            '--+--'    '-----'    '-----'
               |     .-----.
               '---> |  7  |
                     '-----'
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but I don't get why p2.x will not change anything in p. It's right that we have 2 diffrents objects, but they are points to the same x and y (because the shallow copy). What am I missing?
The assignment (in the last two lines) changes what x and y point to. Even if p.x and p2.x have the same value (referring to the same object), they are different variables, and assigning to one does not change the other. (In this case they are not even objects, but primitive values, but the same thing would be valid if they were objects.)
In this example we don't see the difference between shallow and deep cloning.
@PaŭloEbermann if indeed the clone() does do shallow copying , wouldn't the changes to one reflect on the other as well ?I am a little bit confused on this.
@Subhra: If you could change the "value" of the number 1 to something else, this would show in all objects using this number. But numbers (i.e. ints here, assuming java.awt.Point or something similar) are immutable - you can change the content of a variable (per assignment), but not the value of 1 (otherwise it would be quite complicated to write reliable programs).
7

An assignment copies the reference of an instance to a variable. A clone operation will clone the instance (and assign a reference to the clone).

With assignment, you'll end up with multiple variables pointing to one object, with cloning you'll have multiple variables that hold references of multiple objects.

SomeCloneable a = new SomeCloneable();
SomeCloneable b = a;                     // a and b point to the same object

/* versus */

SomeCloneable a = new SomeCloneable();
SomeCloneable b = a.clone();             // a and b point to the different objects

Comments

4

A simple assignment will simply create an Alias for the object. With clone(), each attribute member will also be initialized in the clone Object. However, if the Attribute members are themselves have more objects contained within them, those will not not be copied.

Comments

3

Shallow copy is the default for Object. you can override clone to do a deep copy.

Comments

1

to clone in depth you have to implement Cloneable and override clone()

public class MyClass implements Cloneable {

private Object attr = new Object();

@Override
public Object clone() throws CloneNotSupportedException {
    MyClass clone = (MyClass)super.clone();
    clone.attr = new Object();
    return clone;
}

@Override
public String toString() {
    return super.toString()+", attr=" + attr;
}

public static void main(String[] args) throws Exception {
    MyClass x = new MyClass();
    System.out.println("X="+x);
    MyClass y = (MyClass)x.clone();
    System.out.println("Y="+y);
}

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.