File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String & StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String & StringBuffer" Watch "String & StringBuffer" New topic
Author

String & StringBuffer

Sudha Kris
Ranch Hand

Joined: Oct 31, 2000
Posts: 46
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

Joined: Sep 28, 2000
Posts: 199
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

Joined: Oct 31, 2000
Posts: 46
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
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

 
Don't get me started about those stupid light bulbs.
 
subject: String & StringBuffer