aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clone() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clone()" Watch "Clone()" New topic
Author

Clone()

brent spearios
Ranch Hand

Joined: Apr 10, 2002
Posts: 48
Can someone explain why the value printed out is always no
and also false
class h{
public static void main(String args[]){
String i[][]={ {"hello","world","how"}, new String[7]};
String h[][]=i;


String b[][]=(String [][]) i.clone();
i[0][0]="no";
System.out.println (b[0][0]);
System.out.println (h[0][0]);
//b[1][3]=i[0].length;
//System.out.println (i[0][0]);
System.out.println (b[1][3]);
System.out.println (b[0][0].equals (i[0][0]));
//System.out.println (i[1][2]);
}
}
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm confused. This is the output I get:

Frankly, that's what I expected, as well. One thing to notice about the clone method for arrays is that it creates a "shallow copy" of the original array. That means that the contents of the arrays are not copied, the new array simply references the same objects as the old array. You can find more information about shallow copies in the API for Object.clone.
I hope that helps,
Corey


SCJP Tipline, etc.
brent spearios
Ranch Hand

Joined: Apr 10, 2002
Posts: 48
Thanks, the real problem was what is the point of using clone when you could do a =, i know that it creates a new object when you use clone but what other benefit is there for clone() ?
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Brent,
I've just compiled your piece of code, and the output was

The first "no" comes from the fact that clone creates a new array (that is, a new object), but the string objects still have the same references - so called "shallow cloning" (by contrast, "deep cloning" had to clone all string objects, too, in order to avoid exactly this).
The second "no" comes from h and i pointing to the same array.
The "null" is - obviously - the result of b[1][3] not holding a reference to an object: All elements in an array holding objects are initialized to null, unless specified otherwise.
And the final "true" is the result of b[0][0] pointing to the same reference as i[0][0].
Please let me know if I'm wrong.


Mag
brent spearios
Ranch Hand

Joined: Apr 10, 2002
Posts: 48
Thanks, the real problem was what is the point of using clone when you could do a =, i know that it creates a new object when you use clone but what other benefit is there for clone() ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by brent spearios:
Thanks, the real problem was what is the point of using clone when you could do a =, i know that it creates a new object when you use clone but what other benefit is there for clone() ?

That's the real purpose of the clone method - it created a new object that looks just like the one you've clones. Exactly how that object is created, however, is up to the author of the class implementing the clone method. In the case of an array, you get a shallow copy. If you were to create a class which implemented clone, however, you might want to implement a deep copy, instead. It really all depends on the details of the application.
The purpose of the clone method is to create a new object that looks like the original, as opposed to using the assignment operator (=) which doesn't make a new object, it just assigns a reference to that object to a variable.
Corey
Lee Lee
Greenhorn

Joined: Apr 12, 2002
Posts: 2
Originally posted by brent spearios:
Can someone explain why the value printed out is always no
and also false
class h{
public static void main(String args[]){
String i[][]={ {"hello","world","how"}, new String[7]};
String h[][]=i;


String b[][]=(String [][]) i.clone();
i[0][0]="no";
System.out.println (b[0][0]);
System.out.println (h[0][0]);
//b[1][3]=i[0].length;
//System.out.println (i[0][0]);
System.out.println (b[1][3]);
System.out.println (b[0][0].equals (i[0][0]));
//System.out.println (i[1][2]);
}
}
Lee Lee
Greenhorn

Joined: Apr 12, 2002
Posts: 2
Originally posted by brent spearios:
Can someone explain why the value printed out is always no
and also false
class h{
public static void main(String args[]){
String i[][]={ {"hello","world","how"}, new String[7]};
String h[][]=i;


String b[][]=(String [][]) i.clone();
i[0][0]="no";
System.out.println (b[0][0]);
System.out.println (h[0][0]);
//b[1][3]=i[0].length;
//System.out.println (i[0][0]);
System.out.println (b[1][3]);
System.out.println (b[0][0].equals (i[0][0]));
//System.out.println (i[1][2]);
}
}

Sorry! I just posted a quote without writing anything! This is the first time I write reply in this forum, so please bare with me.
Ok, first of all, I learned a lot by analizing this question, and I'd like to share what I just learned and hope someone can correct me if there is anything wrong with my answer:
First, maybe we should re-phrase the question:
Why AFTER we cloned i, b's contents can still be changed by changing i array's contents?
We know for sure h's contents will be changed whenever i is changed because they have same reference, but why b also changed?
Because, even b != i, but b[x] == i[x], that is, they contains SAME reference!. so b[0] == i[0]
=> b[0][0] == i[0][0]!
so in this case, h and b's contents all got changed!
Try using i[] instead of i[][], then you will see the difference of cloning an object from simply assigning the referece.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clone()