• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refrence Vs Value Objects  RSS feed

 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Value Object is like a DTO/Bean which holds the user input data or data from model and Immutable. So that no one modifies it.

Reference Object are regular objects. Also even if the DTO/Bean which is mutable so that it can be modified by other components then it is called Reference Object.

Is this correct? Can any one add more differences?

Thanks
Chandra.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all i can tell you is when passing parameters, everything in java is pass by value. so that even when you pass a reference(like "this" for example) you are still passing by value because the method gets a copy of the reference. you are not passing it the object but you are passing a separate reference to it. you can change that copy without changing the original.

not exactly what you asked, but along the same lines. i hope i explained that correctly.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid Mr. Twede has it wrong. You pass an object by reference; if the called method changes the values within the object, then they are changed in the caller as well, because the method has a reference to the same object as the caller.


You get the output "a, b" and then "c, d".

If you pass a primitive to a method (int, long, char), then the called method can still change the value it has, but that change is unknown to the caller. Passing a primitive by reference means the caller cannot have the value changed by the called method.

When passing an object reference, the thing the called method cannot do is change the reference in the calling routine; in the example above, the "nameChange" method could have executed the statement "person = new Person();", and then made changes to person, but neither the new object nor the changes made to the new object would have been visible to the caller. If the method did this just before its changes to the name, then the output would be "a, b" and then "a, b" again.

One thing this means is that the only way a called method can create an object and pass it back to a caller is by the method's return value.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pass by value
So repeat after me:

Java is pass-by-value.

(OK, once again... with feeling.)

Java is pass-by-value.

For primitives, you pass a copy of the actual value.

For references to objects, you pass a copy of the reference (the remote control).

You never pass the object. All objects are stored on the heap. Always.

Now go have an extra big cup of coffee and write some code.

and many other references say the same thing

perhaps it is just semantics.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:all i can tell you is when passing parameters, everything in java is pass by value. so that even when you pass a reference(like "this" for example) you are still passing by value because the method gets a copy of the reference. you are not passing it the object but you are passing a separate reference to it. you can change that copy without changing the original.

not exactly what you asked, but along the same lines. i hope i explained that correctly.


I think the bolded part confused me -- "you can change that copy" is correct if you mean "you can change the reference" (as in the "copy of the reference" you refer to earlier). But someone struggling to understand pass-by-value could easily mistake that to mean "you can change the values in the object", and that, of course, is incorrect.

So you didn't really have it wrong, my apologies. Hopefully out of all of this the OP can figure out how it actually works...

rc
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol...i'm not even sure that this was what he was asking about
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:Hi

Value Object is like a DTO/Bean which holds the user input data or data from model and Immutable. So that no one modifies it.

Reference Object are regular objects. Also even if the DTO/Bean which is mutable so that it can be modified by other components then it is called Reference Object.

Is this correct? Can any one add more differences?

Thanks
Chandra.


I think Value Object is what Transfer Object used to be called. I'm not sure that terminology is still used; it's difficult to find pertinent information on the net using "Value Object" as a search term.

Compare:
http://java.sun.com/j2ee/patterns/ValueObject.html
http://www.corej2eepatterns.com/Patterns2ndEd/TransferObject.htm

Not sure where a Reference Object would fit into things, unless it's what is now commonly called a Business Object.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure where a Reference Object would fit into things, unless it's what is now commonly called a Business Object.

that is what i thought also. maybe he is talking about beans which i know nothing about
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to All for the replies
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!