Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

amazing with StringBuffer !! please help

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one tell me why the out put is one more and two instead of one more, one more ?
public class SBtest
{
public static void main(String args[])
{
StringBuffer a= new StringBuffer("one");
StringBuffer b= new StringBuffer("Two");
SBtest.swap(a,b);
System.out.println("A is "+ a +"\nb is " +b);
}
static void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
}
Output :
A is one more
b is Two
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rakesh Patel
try this
public class SBtest
{
public static void main(String args[])
{
StringBuffer a= new StringBuffer("one");
StringBuffer b= new StringBuffer("Two");
SBtest.swap(a,b);
System.out.println("A is "+ a +"\nb is " +b);
}
static void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
System.out.println("in swap A is "+ a +"\nin swap b is " +b);
}
}
And I hope that u can understand this hint.....
Regards
Sameer
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the explanation i think lies in the fact that in method calls, only a copy of an object reference is passed to the called method. if the object itself(not the reference) is modified, the change is reflected in the calling method otherwise the calling method maintains it's original references.
[This message has been edited by Sweekriti Engineer (edited March 30, 2001).]
 
Rakesh Patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sameer and Sweekriti
I got your point. I run the code and according to the explanation by Sweekriti I understood now.
Thank you very much once again.
Rakesh
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rakesh.
I think u got the point...but I am not.
My q. is,While a is changing...why not b???
Can any1 pla.. explain this poit.
Thanks
<marquee> Ratul Banerjee </marquee>
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ratul,
a changes because
a.append changes the content of the stringbuffer whereas
b=a just assigns the reference of a to b.

Hope this helps.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so when the reference to b is changing that means b will now refer to a
that means b will hold what a holds
= one more
so why does it print "two"
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sona:
the reference b ( inside the method ) does channge, but the scope of 'b' inside the method is only in that method, and the 'b' outside remains unchanged, so when you print it displays "two".
Try
the Sameer Sachdeva code to see what happened inside the method.
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code above we have two differents StringBuffer object references called "b"

one outside the method that holds the object created with

new StringBuffer("Two");

and other in the method that also holds
the object created with new StringBuffer("Two");
Now, on the line b = a ;
the second reference called 'b', now holds the value "one more"
but this is a different object reference than the 'b' outside whose value still "two"
the object referenced by a, do change because you are calling a method ( a.append() ) that affect the object
try this:
<code>
replace the swap method
static void swap(StringBuffer a, StringBuffer b)
{
a.append(" more");
b = a;
}
with...
static void swap(StringBuffer alaska, StringBuffer brasil)
{
alaska.append(" more");
brasil= alaska;
}
</code>
the behavior should be the same that in the first code

the object reference called "alaska" still changes the object "one", and the one colled brasil does not change the string "two".
I hope this help and do not create more confusion ( sometimes I confuse my self ;o) )
 
Parimala Somasundaram
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the println statement is outside the swap method, I would expect it to print 'two', but since t is inside the method, should it not print 'one more', because inside the method b has been assigned the reference of a.
Can someone please explain?
Thanks.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parimala Somasundaram:
If the println statement is outside the swap method, I would expect it to print 'two', but since t is inside the method, should it not print 'one more', because inside the method b has been assigned the reference of a.
Can someone please explain?
Thanks.


Hi,
If you look at the code provided by Sameer, you will find it answers your question precisely as you asked / suggested.
For b, the System.out.println() inside swap() will print "one more". Outside swap(), the System.out.println() will print "Two".
Regards,
Lam


 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Funny! I answered this same question on another board earlier with pretty detailed explanation. I'll cut and paste it here and maybe it will help some!
Java always passes by value. The difference between Object values and primitive values is that the "value" in an Object is it's memory location.

When a and b values are passed to the method, there are 2 Object "handles" pointing to each memory location.

a.append("more") appends the String "more" to the memory location referenced by a in the local method (which is the same as a in the main method).

b = a makes the second "handle" drop it's reference to the original b StringBuffer and now it also points to the memory location held by a. Note that if you did a System.out.println(a, b) within the swap() method, you would get "A is one more" "B is one more".

When the method swap completes, and control passes back to the main method both of the second "handles" to the memory location die with the method (and are available for garbage collection because they are unreachable) and what prints is "A is one more" "B is two"


------------------
Lori Battey
SCJP2
 
Parimala Somasundaram
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lam.
Thanks Lori.
I think I've understood it.
 
Rakesh Patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everybody.
I hope the matter was bit confusing but still interesting.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic