• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how object.clone() is different then direct reference assignment

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashMap h1 = new HashMap();
h1.put("one", "one");
h1.put("two", "two");
h1.put("three", "three");
h1.put("four", "four");
h1.put("five", "five");
HashMap h2 = null;
1)//h2 = h1; // Assign reference directly
2)h2 = (HashMap)h1.clone(); // make a clone of the object
h1 = null;

If you try option (1) or (2), both gives the same result, i have tried it. then what is the difference between these two ways [except clone() instantiate a new object with same values]?
Also which one is more efficient and right to use?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Run above code you will find the difference. The difference is that the change in the object refered by h1 is reflected by h2 because it is refering to same object. But the same is not reflected by h3 because it is refering to a different object. Cloning simply means that the values in the object are same but anyway its a different object.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Sumit" please check your private messages about an important administrative matter.

CR
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sumit Bansal:
HashMap h1 = new HashMap();
h1.put("one", "one");
h1.put("two", "two");
h1.put("three", "three");
h1.put("four", "four");
h1.put("five", "five");
HashMap h2 = null;
1)//h2 = h1; // Assign reference directly
2)h2 = (HashMap)h1.clone(); // make a clone of the object
h1 = null;

If you try option (1) or (2), both gives the same result, i have tried it. then what is the difference between these two ways [except clone() instantiate a new object with same values]?
Also which one is more efficient and right to use?



When more than one reference are referring same object (not an immutable one), change made through one reference is reflected through any other.

If my father destroys his house, I will also have my house destroyed; because we are under same roof.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic