• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Clone()

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]);
}
}
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
brent spearios
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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() ?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
brent spearios
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic