aspose file tools*
The moose likes Beginning Java and the fly likes Deep Copy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Deep Copy" Watch "Deep Copy" New topic
Author

Deep Copy

Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 47
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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

Joined: Jul 09, 2010
Posts: 47
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

Joined: Jul 09, 2010
Posts: 47
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 47
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

You'll need to deep copy your roles as well:
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 47
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Jul 09, 2010
Posts: 47
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Jul 09, 2010
Posts: 47
OK. Thanks a bunch for everyone's help I appreciate it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
What's passing by reference? Java™ doesn't support pass-by-reference.
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 47
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deep Copy
 
Similar Threads
struts form being cleared in IE 6
JPA problem: org.hibernate.MappingException: Repeated column in mapping for entity
mapping xml to java using jaxb
how to update OneToMany
EJB 3.0 design question