aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's special about 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 "What Watch "What New topic
Author

What's special about StringBuffer ?

Sudha Kris
Ranch Hand

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

}

}

This gives an output Java2 for just StrinBuffer evenif I don't
return the value back to the calling method. Somebody explained to me that it's 'cos String is mutable & String buffer is not.
I think it's not 'cos of that. If that's so if I pass the string back to the method by making a slight change in the code
public static String Change(String x,StringBuffer y){
x=x.concat("2");
y=y.append("2");
return x;
}

& in the main when I call I assign back s=Change(s,sb); then it prints Java2 & Java2.
I tested it for all primitives,String & StringBuffer, only StringBuffer behaves this way.
So my question is how's StringBuffer managing to retain the values without me returning it back to the calling method.

I hope I am clear in my question.
Thanks
Sudha
Sudha Kris
Ranch Hand

Joined: Oct 31, 2000
Posts: 46
I forgot to ask where in the objectives is StringBuffer? I didn't find any mention to it directly.
Sudha
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
In contrast to the String class that implements immutable strings the StringBuffer class implements mutable strings. When you do StringBuffer append(String sb) the string is appended to the StringBuffer thus changing the original string. This is the reason why even though you don't return the string in the string buffer changes.
Nijeesh Balan
Ranch Hand

Joined: Oct 09, 2000
Posts: 116
Hi Sudha,
I have tried to answer your queries...
First of all
In your first program..
Change(s,sb);
In the method definition Change(String x ,StringBuffer y) the variable x and y are local to the method.
concat()- does not change the original string JAVA. As strings are immutable.. Eventhough u assign the o/p of x.concat("2") to x it is not reflected outside the scope of the method..
append() - modifies the original string "JAVA" to "JAVA2" as StringBuffers are mutable. So there is no question of reference here.
In the second program..
As you are returning the value of x to the original call and reassigning 's', 's' now points to 'x'
so o/p is
String JAVA2 StringBuffer JAVA2
I hope this would be helpful..
Thanks
Nijeesh

Thanks & Regards,<br />Nijeesh.
Sudha Kris
Ranch Hand

Joined: Oct 31, 2000
Posts: 46
My question still remains
why does StringBuffer behave differently.
1. StringBuffer is a class right, so you have to create an instance by 'new'
2. In Change method I am not creating any reference to
StringBuffer y by say new StringBuffer(). Still it appends to "Java".
3. Ok I can understand that, but without 'sb' being static how can it be visible thru out the class.
Hope I am not confusing you.
Thanks in advance
sudha
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hi Sudha,
i'll give it a try....
First i changed to code, used UBB and inserted a line numbering, so that i can refer to some numbers.
People told you String is immutable and StringBuffer is not. That's right, but from reading your questions i guess the problem is not between String and StringBuffer. It's more how parameters especially Objects are passed in Java.

Now step by step:
line 12: Here you create a String s, which is in the String pool
line 13: Here you create an Object of type StringBuffer. To be more precise, you have a variable sb, which is a reference to an object of type StringBuffer.
line 15: Now you call the method Change with parameters s (which is a String) and sb (which is a StringBuffer). Java passes parameters per value, meaning you get a copy of the original variable. Cause String is immutable, here you get a complete new String object. For StringBuffer, you will get a copy of variable sb, BUT this copy points to the same object.
line 3: Here we have a variable x pointing to a totally new String, and a variable y, which points to the same object as sb before.
line 5: Here you create a complete new String, cause Strings are immutable, so x is now another string, which has Java2 as value. On y, which is a reference to the original StringBuffer-Object, you append also 2. So y points now to a StringBuffer-Object which has value Java2.
line 8: Here the method ends, and all local variable are going out of scope. So x and y are no longer valid....
What have you done in this method?? You changed a local variable, which is out of scope at the end of the method -> x
You changed the original StringBuffer-Object over a copy of the reference. y which is this copy is also out of scope at the end of the method, BUT the changes you made on the StringBuffer-Object are still valid.
line 16: The output will be: "String" followed by the value of s, which hasn't changed, cause all changes in method Change were made on a local base ->"Java", followed by "StringBuffer" and then followed by "Java2".
Hope that helps,
correct me if i'm wrong
cheers
Oliver
[This message has been edited by Oliver Grass (edited November 16, 2000).]
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
HI!
Let me try it.
1) You are passing refernces to the the method OK.
2) As string "Java" is padded with "2" it creates a new string in pool, and refers it.
3) Stringbuffer object remains same so the change occurs in original Object.
4) When returning fro the method x and y retain their roiginal refernces so when x is printed it prints "Java" as it still points to the original string in Pool, while y prints the changed value "Java2". because in java objects are passed by refernce by default.
Correct me if I am wrong.
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Sudha,
Ooooh! Mebbe I can also give it a try!!
Your friend was partly right in saying, it is because String is immutable that it is behaving this way, but I guess a much more clear explanation needs to be given...
When you have a StringBuffer, you can alter its value which is not the case with Strings. Actually, when you say,
String s=new String("Sudha");
s+="Kris";
what happens is a memory area for the String "Sudha" is created at a memory location, say 1000. Now when you try to concatenate one more string with it, it creates a new String with value "SudhaKris" at a different memory location, say 2000 and assigns this value to s. But when you do the same with StringBuffer, that is not the case. Whatever change u try to make to the stringbuffer, it is made in that memory location area itself. That is the difference. Now looking back at your code,
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(s);
System.out.println(sb);
System.out.println("String "+s+"Stringbuffer "+sb);
//The output is String Java Stringbuffer Java2
//Whynot String Java Stringbuffer Java ?
}
}
When you call "Change(s,sb)", you are passing the references namely Change(1000,2000) assuming they are getting created at 1000th and 2000th memory locations. Now, when you do a concat on string, it creates a new String whose value is "Java2" whereas when you do an append, it appends it to the value "Java" in the same location. Now you see that the value at 1000 hasn't changed at all, whereas the value at 2000 has changed! That's the reason for no change indicated for String s and a changed value indicated for StringBuffer sb.
Hope that makes things clear.
Aparna

Sudha Kris
Ranch Hand

Joined: Oct 31, 2000
Posts: 46
Thanks a lot Aparna & Oliver.
The memory part cleared my doubts about passing value & reference.
Thanks
Sudha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's special about StringBuffer ?
 
Similar Threads
question:String and StringBuffer
STRING BUFFER query
String & StringBuffer