• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Deep Copy

 
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I know that there have been plenty of question regarding deep copying, but I just don't get it and I'm getting brain fried trying to figure it out. I understand the purpose behind the concept but not how it's done. From what I've read the copy constructor seems to be the best choice, so if I had:


(and I'm not sure if the copy constructor is correct), how would I use the copy constructor to make deep copies in the getter methods? I know you don't have to for int and String because they're primitive/immutable, but I want to make sure I understand how the deep copy works.

TIA,
GyJ
 
Marshal
Posts: 80767
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

How about this?Please maintain full indentation and new lines when you post code; you are on screen not paper, so there is no limit to the number of lines available.
Why have you got a no-args constructor? That appears dangerous because it will allow null values in User objects. Your get and set methods for userRoles are named inappropriately; they should be called getUserRoles and setUserRoles.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice Campbell. I made the no-args constructor because at one point the compiler was griping about not having a default constructor and because I was copying an example but whatever code isn't used will be removed.

So let me see if I have this right, if I use

for the user roles, then theoretically I could also do

for the user id and

for the user name and that will technically provide deep copies of user id and user name as well, right? And to make sure I have this right, the statements

return a completely new (and different) variable, and the return new HashSet statement is the same as

And if all of the above is true, then it seems to me that the copy constructor is only needed if I wanted to return an object of the User rather than the individual pieces, is that correct and am I starting to see the light at the end of the tunnel (or is it a train)?

Thanks.
GyJ
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice Campbell. I made the no-args constructor because at one point the compiler was griping about not having a default constructor and because I was copying an example but whatever code isn't used will be removed.

So let me see if I have this right, if I use

for the user roles, then theoretically I could also do

for the user id and

for the user name and that will technically provide deep copies of user id and user name as well, right? And to make sure I have this right, the statements

return a completely new (and different) variable, and the return new HashSet statement is the same as

And if all of the above is true, then it seems to me that the copy constructor is only needed if I wanted to return an object of the User rather than the individual pieces, is that correct and am I starting to see the light at the end of the tunnel (or is it a train)? To be sure, if I did

would that show user and userRoles at different addresses?
Thanks.
GyJ
 
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer to return Collections.unmodiableSet(userRoles) mostly. That doesn't copy all elements, but still disallows modification from outside. If calling code then needs a copy they can do that themselves:
I of course specify in the Javadocs that the returned Set is unmodifiable.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm expecting a deep copy that does

Would display something like

instead of

Doesn't the identical values indicate that this is a shallow copy?

Thanks.
 
Rob Spoor
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to deep copy your roles as well:
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, but since I'm still at a loss what goes at ... ? I've been trying variations of .add but I keep coming up with the same results of a shallow copy.
 
Rob Spoor
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I can't find any class called com.hibernate.login.Role I can't help you with that. The only occurrence of it on Google is this thread! And I also cannot find any class called Role in the entire Hibernate API.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, your response did help. And I now have the deep copied results. Thanks a lot, but I must ask...what is the advantage of passing by reference when it seems that passing by value is more desirable in regards to data protection?
 
Rob Spoor
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you don't need to protect your data like that. The Map returned by ProcessBuilder.environment() is such an example - you change the environment by modifying this Map directly.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Thanks a bunch for everyone's help I appreciate it.
 
Campbell Ritchie
Marshal
Posts: 80767
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's passing by reference? Java™ doesn't support pass-by-reference.
 
Emil Jennings
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen enough about the Java only passes by value, no passing by reference, so I'll answer with the below quote from Murach's Java SE 6, Joel Murach and Andrea Steelman, 2007, Mike Murach & Associates, Inc. pp 200-201:

Figure 6-12 shows that primitive type are passed to a method one way, while reference types (objects) are passed another way. Specifically, primitive types are passed by value, which means that a copy of the value is passed, not the value itself. In contrast, objects are passed by reference, which means that a pointer to the object is passed. Because of that, the method knows where the object's variables are so it can change them directly.

Figure 6-12:
Example 1: Primitive types are passed by value

Code that calls the method

Result

Example 2: Objects are passed by reference

Code that calls the method

Result

Description:
When a primitive type is passed to a method, it is passed by value. That means the method can't change the value of the variable itself. Instead, the method must return a new value that gets stored in the variable.
When a reference type (an object) is passed to a method, it is passed by reference. That means that the method can change the data in the object itself, so a new value doesn't need to be returned by the method.
 
Rob Spoor
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gunny Jennings wrote:Example 2: Objects are passed by reference


No they aren't. Or at least, the reference is being passed by value. http://faq.javaranch.com/java/CallByReferenceVsCallByValue
 
Campbell Ritchie
Marshal
Posts: 80767
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gunny Jennings wrote: . . .
Description:
When a primitive type is passed to a method, it is passed by value. That means the method can't change the value of the variable itself. Instead, the method must return a new value that gets stored in the variable.
When a reference type (an object) is passed to a method, it is passed by reference. That means that the method can change the data in the object itself, so a new value doesn't need to be returned by the method.

Murach are capable of far better than that.

That's nonsense. Everything is passed by value. If it were pass-by-reference you could change the actual object. You can't. It is pass-by-value so you can't change the object, but you can manipulate it (ie change its state).
Start with this link, and do a search through these fora for pass by reference and pass by value. You will find threads like this one where we spent hours and hours trying to demonstrate the difference.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic