Results 1 to 19 of 19
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default 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.....

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jomypgeorge View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    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?

  4. #4
    ron2794 is offline Member
    Join Date
    Dec 2010
    Posts
    38
    Rep Power
    0

    Default

    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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jomypgeorge View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by jomypgeorge View Post
    but what if i need to change my string frequently? should i use string buffer?

    yes. if you don't need thread safety use the StringBuilder.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    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?
    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:
    Java Code:
    String something = "This is my string " + somevar + " which is made from " + someOtherVar + " lots of " + vars + ".";
    uses a builder (or is it buffer?) since the compiler can spot these things.

    Anyway, if your Strings are not being built up, then stick to Strings.

  8. #8
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks for you all
    but can u give an example for thread safety in string buffer?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  10. #10
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    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();
    }
    }

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    That buffer's not shared between threads.
    If it was static it would be.

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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();
    	}
    }

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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...:)

  14. #14
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    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?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  16. #16
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    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 12:42 PM.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  18. #18
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks

    so string buffer synchronize only the altering operation to it. not the entire method that try to alter it...right?

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    StringBuffer can only synchronise its own stuff.
    How can it reach out to the code around it?

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  2. Strings and Immutable
    By al_Marshy_1981 in forum New To Java
    Replies: 19
    Last Post: 06-18-2010, 08:22 AM
  3. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 01:58 PM
  4. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 09:12 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •