• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

String & StringBuffer

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code.IMO the output should have been
"String Java Stringbuffer Java"
But I am getting String Java Stringbuffer Java2"
Why is it, I know String is immutable,but the method is not returning any value & the String & Stringbuffer are not static.But still how can the output be like that?

public class str{
public static void Change(String x,StringBuffer y){
x=x.concat("2");
y=y.append("2");

}
public static void main(String args[]){
String s = "Java";
StringBuffer sb = new StringBuffer("Java");
Change(s,sb);
System.out.println("String "+s+" Stringbuffer "+sb);
//The output is String Java Stringbuffer Java2
//Whynot String Java Stringbuffer Java ?

}

}

Can anybody comment on this,
Thanks
Sudha
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sudha,
Did u forget that StringBuffer is mutable...??? Unlike String.
The append method appends 2 to Java & hence ur result.
HTH,
Aruna
 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I know that but the point is
-there's no return value &
- these variables are not class variables
so how will this Stringbuffer value change?
Maybe I still have 'C' hangover, but did anybody got my point?
After going thru' many books incompletely I am really confused now.
Thanks
Sudha
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You passed the reference to the Stringbuffer to the Change method. It went to that address and added on the 2. It didn't need to return a value to do that.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer object contains a block of memory to store strings so when you call the append method it just adds up the strings one behind the other(I say this so that you can visualise what exactly happens)..I hope this explanation helps.

Originally posted by Sudha Kris:
In this code.IMO the output should have been
"String Java Stringbuffer Java"
But I am getting String Java Stringbuffer Java2"
Why is it, I know String is immutable,but the method is not returning any value & the String & Stringbuffer are not static.But still how can the output be like that?

public class str{
public static void Change(String x,StringBuffer y){
x=x.concat("2");
y=y.append("2");

}
public static void main(String args[]){
String s = "Java";
StringBuffer sb = new StringBuffer("Java");
Change(s,sb);
System.out.println("String "+s+" Stringbuffer "+sb);
//The output is String Java Stringbuffer Java2
//Whynot String Java Stringbuffer Java ?

}

}

Can anybody comment on this,
Thanks
Sudha


 
reply
    Bookmark Topic Watch Topic
  • New Topic