• 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

please help

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



---------- Interpretation(Java) ----------
S1 Before Append :Hello
S1 After Append :Hello how are you
S1 after s1=s2 :doing
Hello how are you

Output completed (0 sec consumed)



Question1: why iam not getting Output on the lastline as "doing"
Question2:As s1 in main() and s1 in mybuf are different why append is changing the value of s1 in main
Question3:am i passing the value of s1 and s2 by ref or not ,i suppose i am passing value as refrence as s1 and s2 are objects. not values then how come value iam changing in testbuffer2 class is not propagated to my main class.
[edit]Add code tags, remove some smilies. CR[/edit]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch
Please don't simply write "please help" as a thread title: look at this FAQ. And please use the CODE button. I have edited your post and you can see how much better it looks.

As you say, Java is entirely "pass by value." I wrote about that yesterday, and have a look at this old thread, and this JavaRanch FAQ. They might be helpful. Look at the links I quoted in that old thread.

You have s1 and s2 in your main method, then you pass copies of their location to the myBuf method. So the myBuf method has values, which it calls s1 and s2. When you reassign them in myBuf, you are only reassigning the local copies. Remember pass-by-value cannot change the values passed, so the original locations of the two StringBuffers are unchanged. So when you go back to the main method, you go back to the original s1 there.

Try changing the names of the parameters in myBuf to s3 and s4. You might then find it easier see that changing s3 doesn't change the original s1.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 2: When you pass the original reference to s1, you are allowing your myBuf method access to a mutable class. You cannot change the reference pointed to, but you can manipulate the object in that reference. In this case you are using its append method, which does not change which object you are using. But it does alter the state of that object.

By the way: use StringBuilder rather than StringBuffer unless you specifically need synchronization.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bartender

Praveen
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic