Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shallow Cloning

 
Shivani Yadav
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Jack Bento
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this in the exam?
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed, interesting topic, not on the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic