• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing variable into methods

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I am trying to confirm my understanding of this section of the Kathy & Bert v6 book. On page 213, for the example listed on pg 214 (passing object reference variable):

import java.awt.Dimension;
class ReferenceTest {
...
Dimension d=new Dimension(5,10);
...

//height member value is 10 here
rt.modify(d);

..println("..".+dim.height); //height still remains 11
}

void modify(Dimension dim) {
dim.height=dim.height+1; //height becomes 11 here
....
}

Does height member still retain value of 11 (after modify() invocation) because I am making a direct reference change to member height?

As opposed to pass-by-value semantics as on p 215,

void bar() { Foo f=new Foo(); doStuff(f); }
void doStuff(Foo g) { g.setName("Boo"); g=new Foo(); }

In another way to reword the book, is this assumption correct?
I cannot change any f/g member in doStuff() since it is not making a direct reference change and doing it through a setter like setName().

I am just trying to find my own way to understand this difference?
Thanks

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to run this code to confirm your assumption



What heppens to f's name value i.e. whats the output of this program

 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks anyhow but this does not answer my original question. This only adds to my confusion. I am looking for a simple answer or explanation.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does height member still retain value of 11 (after modify() invocation) because I am making a direct reference change to member height?



Yes ! it will be 11 ....
I thinks you have misinterprited the whole thing...
pass-by-value and pass-by-reference...

pass-by-reference
when you pass a abject reference in a method (as you did doStuff(f); ) JVM will create a new copy of the referece...
i.e. 2 references pointing to same object
so what it mean
whatever changes you do in the method with object thru new object reference it also reflected origianl object state...because object is still common for both references
but if you change the reference (as you did g=new Foo(); ) now after this line 2 objects will be there...
and both the references are pointing to 2 diffrent objects...
After this statement you won't be able to change the state of original object...

pass-by-value
it is as if you are passing a primitive value like int,long...in this is case too JVM will create new copy of the variable value..

However both the cases are of pass-by-reference

its debat java supports what the pass-by-reference or pass-by-value and i don't want to go in to that...
 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. As in C/C++, there is a clear distinction between the 2 between the dot operator and -> operator. There seems to be no clear distinction you can see in Java code. I am just trying to distinguish between the 2 just by looking at Java code or is this possible? I do understand the difference but just need to see it in the actual code.
Thanks
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rich tana wrote:Thanks. As in C/C++, there is a clear distinction between the 2 between the dot operator and -> operator. There seems to be no clear distinction you can see in Java code. I am just trying to distinguish between the 2 just by looking at Java code or is this possible? I do understand the difference but just need to see it in the actual code.
Thanks


The only thing you need to understand is that Java exclusively uses pass-by-value.

Whether you pass a primitive or a reference variable, the bit pattern on the argument variable at the point of the call is copied on to the parameter variable in the method.

For primitive variables, this means that you are just copying the value of the primitive to a new variable, and any changes made to the parameter variable inside the method will not affect the argument variable.

For reference variables the situation is just slightly more complex. The bit pattern in a reference variable is a reference to a specific object in the heap (think of it as a pointer in C/C++). Therefore, the parameter variable in the method will point to the same object as the argument variable at the point of the call. This means that if the object is mutable, you can change its state through the method, via the parameter. However, if you rebind the parameter to point to a different object (or assign null to it) this will leave the argument variable unaffected (it will still point to the same object is pointed before the method was called.) The point is that when you pass a reference variable to a method, the method can potentially modify the object the variable points to, but the variable can't be modified (in that it will remain pointed to that same object.)

The reason you might be a little confused is because the semantics of variables in C++ is different from the semantics of variables in Java. In C++, when you pass a variable, you pass a copy of the object itself (pass by value in C++.) In Java, when you pass a reference variable, you pass a copy of the reference variable itself (pass by value in Java.) You can also think of it as this: C++ treats reference variables the same as primitive variables (the variable denotes the value of the object itself) whereas in Java, a primitive variable "contains" the value of "the object" itself, whereas a reference variable simply refers to the object. This is a little sketchy, but maybe it will help you understand how things work.
 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again, thanks Ruben. Is it possible to provide an example of the syntax you are explaining? This will clear it up pretty quickly.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rich tana wrote:Once again, thanks Ruben. Is it possible to provide an example of the syntax you are explaining? This will clear it up pretty quickly.


Here you go Rich:

 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You rule Ruben. Thanks so much! Now, here is another question. If I change to:



The result should of: System.out.println("Value of reference y parameter after increment is: " + y.x);
should be y=1 instead? If so, I definitely got it!!

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rich tana wrote:You rule Ruben. Thanks so much! Now, here is another question. If I change to:



The result should of: System.out.println("Value of reference y parameter after increment is: " + y.x);
should be y=1 instead? If so, I definitely got it!!


Hi Rich,

What do you mean by setInc(x)? That would not work, since there is no x static variable in the MethodCall class.
 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops. Sorry typo. Here it is corrected:

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get the same result with this method chaining. You can change data using a method, but not the references.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rich tana wrote:Oops. Sorry typo. Here it is corrected:


Yes, this should work the same. The state of the object is modified in the second method, but the object is still the same, so the change takes place through any number of method calls.
 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to be a dodo Ruben, but this brings me back to my original confusion, why does this not work then?


As opposed to pass-by-value semantics as on p 215,

void bar() { Foo f=new Foo(); doStuff(f); }
void doStuff(Foo g) { g.setName("Boo"); g=new Foo(); }

In another way to reword the book, is this assumption correct?
I cannot change any f/g member in doStuff() since it is not making a direct reference change and doing it through a setter like setName().
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rich tana wrote:Sorry to be a dodo Ruben, but this brings me back to my original confusion, why does this not work then?


As opposed to pass-by-value semantics as on p 215,

void bar() { Foo f=new Foo(); doStuff(f); }
void doStuff(Foo g) { g.setName("Boo"); g=new Foo(); }

In another way to reword the book, is this assumption correct?
I cannot change any f/g member in doStuff() since it is not making a direct reference change and doing it through a setter like setName().


No worries Rich, all questions are legitimate. But I don't understand what your question is. You could change the state of f in doStuff() directly modifying any public instance variables on it as well. However, if the instance variables are private, the only way to modify the state is through a setter (this is in essence encapsulation at work, which also enables loose coupling.)
 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AS with the code from below (K&B book pg 215):



It says you cannot change the state of f or I am proving to be seeing things again. Maybe I am putting way too much effort into this?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rich tana wrote:AS with the code from below (K&B book pg 215):



It says you cannot change the state of f or I am proving to be seeing things again. Maybe I am putting way too much effort into this?


OK, I think I know what they mean. They mean that you can change the state of the object that f points to like this:

g.setName("Boo");

But you can't make f point to a different object like this:

g = new Foo(); // This only rebinds parameter variable g, argument variable f is unchanged.

Does that clarify things now? You can not change the original reference variable f inside the method, since its value (basically the bit pattern which points to a specific object) was simply copied in the parameter variable g when the method was called. If this doesn't make sense, reread my previous posts, and take a break of a few hours, then look at it again. The information is in there.
 
rich tana
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely makes sense, I see the difference.
Big thanks to all including Ruben.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic