Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Pass-by-Vaule Object reference question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was glancing over Sierra/Bates Java 2 cert book and came across the following code example explaining pass-by-value semantics:

void bar()
{
Foo f = new Foo();
doStuff(f);
}

void doStuff(Foo g)
{
g = new Foo();
}


From this code:

1. are g and f both referring to the SAME object? or are they 2 separate instances?

2. if g's object is somehow modified from whatever value f passed to it, will f's object also be modified?


I'm confused, because from the code, 2 different instances are created, but since f is passed into g, i'm not sure if that means that both variables are pointing to the SAME object?

Thanks in advance for your help.

JP
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference variable g is a copy of the reference variable f. That's a copy of the reference, not the object. At the time of the method being invoked they are pointing to the same instance. As soon as the first line in doStuff executes a reference to a new and different instance is assigned to the variable g and it now points to a different instance. The first variable, f, is not changed in any way precisely because g is a COPY of f and not a "reference" (in quotes because now we're not talking about a Java reference) to f.
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:
The reference variable g is a copy of the reference variable f. That's a copy of the reference, not the object. At the time of the method being invoked they are pointing to the same instance. As soon as the first line in doStuff executes a reference to a new and different instance is assigned to the variable g and it now points to a different instance. The first variable, f, is not changed in any way precisely because g is a COPY of f and not a "reference" (in quotes because now we're not talking about a Java reference) to f.



For more clarifications check out this thread.

Object Pass by Value Vs Reference

Hope this helps you to understand better.
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ken,

Ok, I understand that, but let's say the doStuff method was like this:

class Foo
{
int height = 10;


void bar()
{
Foo f = new Foo();
doStuff(f);
}
doStuff(Foo g)
{
g.height = g.height + 1;
}

will the object's height value that f is referring to be 11 also?

Thanks,

JP
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Park:
Thanks Ken,

Ok, I understand that, but let's say the doStuff method was like this:

class Foo
{
int height = 10;


void bar()
{
Foo f = new Foo();
doStuff(f);
}
doStuff(Foo g)
{
g.height = g.height + 1;
}

will the object's height value that f is referring to be 11 also?

Thanks,

JP



Yes, because g and f are referring to the same object.
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool, thanks for the help.

I was a bit confused with pass by values using object references, but i think i got it now..

basically whenever an object reference is passed into another object reference, they will possess the same reference to the SAME object..however, if the new object reference instantiates a new instance, they become referenced to 2 differnt objects..so whatever happens to the new reference..the reference which was orginially passed in will not be affected...?

Thanks,

JP
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Park:
cool, thanks for the help.

I was a bit confused with pass by values using object references, but i think i got it now..

basically whenever an object reference is passed into another object reference, they will possess the same reference to the SAME object



No. They are two different references to the same object. That is key, "g" is a COPY of "f" -- they aren't the same reference, they just point to the same object.

Originally posted by John Park:
..however, if the new object reference instantiates a new instance, they become referenced to 2 differnt objects..so whatever happens to the new reference..the reference which was orginially passed in will not be affected...?

Thanks,

JP



Since "g" is not "f", they are two completely different references. The value of f is copied to g when the method is invoked. If in your method you change the value of g, for example through assignment, that has no effect on f.
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, now i'm a bit confused...

Ken, with what you said above, what about this example then?

class Foo
{
int height = 10;


void bar()
{
Foo f = new Foo();
doStuff(f);
}
doStuff(Foo g)
{
g.height = g.height + 1;
}

the object that f is pointing to, will also be affected, since f and g both point to the same object....am i missing something here?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,


Till tag [1] both the references are pointing to the same object. Hence any changes made to the object pointed by g will also be for the object pointed by f as they point to the same object.
After line at tag [2] is executed the reference g starts pointing to the new object and hence any changes made to this new object pointed by g will not affect the object pointed by f.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Park:
ok, now i'm a bit confused...

Ken, with what you said above, what about this example then?

class Foo
{
int height = 10;


void bar()
{
Foo f = new Foo();
doStuff(f);
}
doStuff(Foo g)
{
g.height = g.height + 1;
}

the object that f is pointing to, will also be affected, since f and g both point to the same object....am i missing something here?



Yes, that's right. Two different references pointing to the same object. Key here is to understand when we're talking about the reference and when we're talking about the object. The object and the reference are two distinct entities here, a reference isn't just an "alias" like in C++, it's completely separate. So a change in one reference's value will not affect a different reference, but a change in the object it points to is going to be seen elsewhere if other references also point to that same object.
 
John Park
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotcha. Thanks to all those who contributed.


JP
 
Arthur, where are your pants? Check under this tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic