File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Doubt in Parameter Passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Parameter Passing" Watch "Doubt in Parameter Passing" New topic

Doubt in Parameter Passing

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi Folks,

Please note the following program:

It is giving the following output:

S1 Before Append : Hello
S1 After Append : Hello How are you
S1 After S1=S2 : Doing
Final Output in Main Method : Hello How Are you.

Could you please explain me why it is NOT printing "Doing" in the last output (it has been changed to "doing" in the method myBuf() ?
In the statement "s1=s2", if s1 and s2 are local to the method myBuf(),
then why in the statement s1.append("how are you") , s1 is not local to the method myBuff().

I am a bit confused. A good explanation is GREATLY APPRECIATED.

Thanks in advance

Best Regards

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

s1=s2; in the method is modifing the reference of s1 which is created in the method and that s1 is not same as the reference in the main method

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
We have an FAQ which may help.
I agree. Here's the link:
subject: Doubt in Parameter Passing
It's not a secret anymore!