• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String & StringBuffer

 
Sudha Kris
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Aru Ven
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Roopa Bagur
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic