• 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

argument passing

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, all,
It is very difficult for me to understand the following examples of object arguments passing in Simon's book:
Example1:
Button btn;
btn = new Button("Pink");
replacer(btn);
System.out.println(btn.getLabel());
public void replacer(Button replaceMe) {
replaceMe = new button("Blue");
}
The author said in this example, the value printed out is still Pink, not Blue.
Example 2:
Button btn;
btn = new Button("Pink");
changer(btn);
System.out.println(btn.getLabel());
pbulic void changer(Button replaceMe){
changeMe.setLable("Blue"));
}
The author said that the printed value for this code is Blue.
My understanding is that when an argument is a primitive data type and passed into a method, changes to the argument value by the method do not affect the original data; while if it is object reference, the original value will be affected when passing value by method call.
I am not sure whether my understanding is correct.
Thanks! luk
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luk,
I'll try to exmplain. Let's look at first one:
Button btn;
btn = new Button("Pink");
replacer(btn);
System.out.println(btn.getLabel());
public void replacer(Button replaceMe) {
replaceMe = new button("Blue");
}
See what's happening is this. btn is a reference to a Button with label pink. WHen method replaces is called it passes it a reference btn. But look what's happening there. btn is assigned to a replaceMe now it points to that same Button object right but look there it's been assigned a new instance of another button so it has not modified the object that btn is still point too. If it would have called method setLabel replaceMe.setLabel("wahtever") now the label that btn object points to would have been modified. But it's not it just get's assgined to point to a new created object.

Example 2:
Button btn;
btn = new Button("Pink");
changer(btn);
System.out.println(btn.getLabel());
pbulic void changer(Button replaceMe){
changeMe.setLable("Blue")); // shouldn't changeMe be replaceMe instead ?
}
See that's what i talked about in earlier comment . This is the exact behaviour that's happening here.
 
luk Hann
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, val,
Thanks so much for your quick reply and answer.
I thought I was still a little bit "clear as mud". Could you illustrate the following from another angle?
(btn is assigned to a replaceMe now it points to that same Button object right but look there it's been assigned a new instance of another button so it has not modified the object that btn is still point too).
Can I say that the object Button has two references: btn and replaceMe with two values: "pink" and "blue". I think something did affect the result here are getLable and setLable method. Using only new constructor could not pass a value to btn, while using setLable method could. Is that right?
In Simon's explanation, he also mentioned "caller" and "callee". Which is caller, which is callee in this example?
Finally, what happened with array?
Sorry to answer so many questions and thanks so much in advance!
Luk

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope this diagram helps out.
variable replaceme variable Btn
| |
____________________________
| physical object Button |
| |
| has a label property |
|___________________________|
Notice that replaceme and Btn are only variables that point to
the physical object and the physical object has a property say
a label property.
In passing parameters for objects, only a copy of the address
is sent. So replaceme is just a pointer to the physical
object.
When modifying passing object parameters in this case
replaceme, the address of replaceme cannot be changed. That's
the catch the physical object that replaceme points to
cannot change. But the properties of the object pointed to
can change. That is the reason why the label can be changed,
and a new button cannot be created.
When a new button is created, and replaceme points to it, it
changes the original address of the object.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this topic. I to had problem with the exact same question.

------------------
Preparnig for the Java 2 Certification exam
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charlie, I agree with everything you said except maybe your final sentence:


When a new button is created, and replaceme points to it, it
changes the original address of the object.


The original address of the object (the button) is not being changed; rather, the address which the reference variable replaceMe is pointing to is changed to point to a new button (which has no effect on the original button object). And since you've now lost your only pointer to the original button within the replacer() method, nothing that follows in that method can possibly affect the original button.
reply
    Bookmark Topic Watch Topic
  • New Topic