Results 1 to 19 of 19
Thread: String Immutable
- 12-17-2010, 07:02 AM #1
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
String Immutable
Hi friends,
i have a doubt regarding string immutability. i know String is immutable class, so its value cannot be changed
so
String str1 = "hai";
str1 = "hello";
then a new string object is created and first one will be eligible for garbage collection. its something internal and we use str1 as the reference.
what i am asking is is there any limitation in string then string buffer?
any thing that cannot do in string but possible with string buffer
i know string is much slow on modifications... any thing apart from that?
thanks in advance.....
- 12-17-2010, 07:19 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
If you read the API documentation for both the String and StringBuffer (StringBuilder) classes you'll see the differences; one difference that comes to mind is the reverse() method, i.e. you can reverse an entire StringBuffer (StringBuilder) but a String can't do it. The String class has a lot more conversion methods though.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-17-2010, 07:45 AM #3
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks Jos
so where should i use string buffer over string?
i don't need any methods in string buffer. i can handle all my operations with string itself.
but what if i need to change my string frequently? should i use string buffer?
- 12-17-2010, 07:49 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 38
- Rep Power
- 0
string buffer is something where we need to append the data instead of assigning a new value
eg:
String s=''sdf";
next when in any method you assign it as :
s="asd";
s will have the value asd
but if it is a string buffer than you can append its value
eg:
s.append("asd");
and the string will become
sdfasd
- 12-17-2010, 08:03 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
A StringBuffer is to a StringBuilder as a Vector is to an ArrayList; the first classes synchronize all their buffer access (get, set, insert, remove). The unsynchronized versions are a bit faster. Fiddling with Strings is almost always slower than using a buffer/builder.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-17-2010, 09:26 AM #6
- 12-17-2010, 09:40 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
As said, a StringBuffer (or StringBuilder) is for building a String. That is, it's generally for constructing a longer String from various bits of data you have.
If you are not builsing a String then there is no real need for a StringBuilder/Buffer. Indeed, unless you;re building a String in several stages then there's no need for one either as:
uses a builder (or is it buffer?) since the compiler can spot these things.Java Code:String something = "This is my string " + somevar + " which is made from " + someOtherVar + " lots of " + vars + ".";
Anyway, if your Strings are not being built up, then stick to Strings.
- 12-17-2010, 09:41 AM #8
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks for you all
but can u give an example for thread safety in string buffer?
- 12-17-2010, 09:46 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Again, that only applies if you are using the same builder across multiple threads. This is a very rare occurrence. I haven't seen it in anything I've written in 10 years.
- 12-17-2010, 10:09 AM #10
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thank Tolls,
i tried one just for applying it
//************************************************** *****
class String1Eg implements Runnable
{
StringBuffer str1 =new StringBuffer( "hai");
public void run()
{
for (int i =0; i<10;i++)
{
str1 = str1.append(i);
System.out.println(str1);
}
}
public static void main(String ... args)
{
String1Eg obj =new String1Eg();
Thread t1 = new Thread(obj);
Thread t2 = new Thread(obj);
Thread t3 = new Thread(obj);
t1.start();
t2.start();
t3.start();
}
}
- 12-17-2010, 10:12 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That buffer's not shared between threads.
If it was static it would be.
- 12-17-2010, 10:32 AM #12
Java Code:class String1Eg implements Runnable { StringBuffer str1 ; public String1Eg(StringBuffer sb) { str1 = sb; } public void run() { for (int i = 0; i < 10; i++) { str1 = str1.append(i); System.out.println(str1); } } public static void main(String[] args) { StringBuffer sb1 = new StringBuffer(); String1Eg obj1 = new String1Eg(sb1); String1Eg obj2 = new String1Eg(sb1); // obj1 and obj2 will share the same StringBuffer Thread t1 = new Thread(obj1); Thread t2 = new Thread(obj2); t1.start(); t2.start(); } }
- 12-17-2010, 10:49 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Ah, hang on.
I was wrong earlier.
I misread your code jomypgeorge. I thought they were individual instances of String1Eg, and not a single instance that was launched on several threads.
So yes, that's an example of a single buffer being used by multiple threads.
Not to self: Read code properly...:)
- 12-17-2010, 11:09 AM #14
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
i tried it but got result as
0
00
0011
0011
001122
001122
00112233
00112233
0011223344
00112233445
001122334455
0011223344556
00112233445566
001122334455667
0011223344556677
00112233445566778
001122334455667788
0011223344556677889
00112233445566778899
00112233445566778899
some lines are repeating .... but why?
- 12-17-2010, 11:25 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Look at this bit:
001122
001122
00112233
00112233
Between the 001122 and the first 00112233 two numbers were added to the buffer. Which means the code switched from one thread to the other before doing the println(), then did one of the println()s and then the other. Resulting in two lines the same.
- 12-17-2010, 11:39 AM #16
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
Thanks Tolls
so only the append() is got synchronized.
how can we make it print without conflict?
i got same result using string builder in above program....
what was the reason?Last edited by jomypgeorge; 12-17-2010 at 11:42 AM.
- 12-17-2010, 11:46 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Your code is not synchronised so the JVM can interrupt a thread between:
str1 = str1.append(i);
and
System.out.println(str1);
In the case of the builer it's considered unsafe since it could interrupt in the middle of doing the append(). Not knowing that code I couldn't say what potential damage could be done, but I suspect you could end up with an inconsistent String. That it didn't happen to you (and I wouldn't know how to test that) is simply luck. It might be a really really unlikely event.
- 12-17-2010, 11:51 AM #18
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks
so string buffer synchronize only the altering operation to it. not the entire method that try to alter it...right?
- 12-17-2010, 11:54 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
Strings and Immutable
By al_Marshy_1981 in forum New To JavaReplies: 19Last Post: 06-18-2010, 07:22 AM -
What is Immutable in String
By elektronika in forum New To JavaReplies: 4Last Post: 12-10-2009, 12:58 PM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 PM -
Strings are immutable yet they can be changed ?
By anjanesh in forum New To JavaReplies: 4Last Post: 05-19-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks