• 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

Shallow Cloning

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having some confusion in understanding shallow clonning. Just want to clear my understanding.

As per shallow clonning ..."In shallow copy the object is copied without its contained objects.
Shallow clone only copies the top level structure of the object not the lower levels..."

I just tried the below code to see practically.



In the above code i am having a query.
When i am doing " clonedObj.str1 = "String33"; " then why the origObj.str1 value is not changed? i.e. origObj.str1 is "String1" and not the "String33". Here are they not referring to the same String refrence?

This is not happening when i am doing "clonedObj.fsh.setName("Octopus");" i.e origObj.fsh name also gets modified as they are referring to the same Fish object.

Can anybody please explain me the difference between above two scenarios.

Thanks in advance.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In first case, you are changing the "Fish" object's name, but both "Shallow" objects refer to the same "Fish" object. But with "str1" reference though both objects initially refers to the same, changing one's reference doesn't change the other's (that is, Java uses "pass by value" so str1 of the object you changed will refer to the new String object ).
 
Shivani Yadav
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks, i got it.

When i am setting the name of the Fish object, i am not changing the object to which clonedObj.fsh and origObj.fsh is referring to. Only the object's state is changed.

But when i am assigning value to clonedObj.str1 the actual object to which it was referring is changed so i will see the difference.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So while cloning a separate shallow object is created but the same fish object is used by both the shallow objects(the clone one and the parent).Why is that so?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PrasannaKumar Sathiyanantham wrote:So while cloning a separate shallow object is created but the same fish object is used by both the shallow objects(the clone one and the parent).Why is that so?


That's how shallow copy works (clone() in Object class), It only copies object references. If you want deep copy (it's own copies for the cloned object) you should override the clone() in your class.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this in the exam?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fray Bentos wrote:Is this in the exam?


No, nothing about cloning on the SCJP exam. Check the Objectives for SCJP 6.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed, interesting topic, not on the exam.
 
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic