Explain difference between String and StringBuffer ?

Discussion in 'Strings, I/O, Formatting, Parsing' started by austin.premkumar, Sep 26, 2008.

  1. austin.premkumar

    austin.premkumar New Member

    Hi I need to know difference between string and string buffer.

    I know some basic things.
    String --immutable(if we trying to change a value of this string means we won`t change the old values instead of that the new value get stored on one new memory space)
    StringBuffer--mutable(but here we change that value where that old value was stored)
    Apart from that there is any difference between those twos.
    And also tell me what is String Builder?
     
  2. vorugantisanthosh

    vorugantisanthosh New Member

    Hi Premkumar,

    String is fixed length & immutable character sequence
    StringBuffer is growable& writeable character sequence
     
  3. vbraojava

    vbraojava New Member

    Re: Explian Difference between String and StringBuffer ?

    Hi Premkumar,

    String : String is immutable you can’t modify a String object but can replace it by creating a new instance. Creating a new instance is rather expensive.

    Example :

    String output = “Some text”
    Int count = 100;
    for(int I =0; i<count; i++) {
    output += i;
    }
    return output;

    - The above code would build 99 new String objects, of which 98 would be thrown away immediately. Creating new objects is not efficient.

    StringBuffer : StringBuffer is mutable, use StringBuffer when you want to modify the contents.

    Example :

    StringBuffer output = new StringBuffer(110);
    Output.append(“Some text”);
    for(int I =0; i<count; i++) {
    output.append(i);
    }
    return output.toString();

    - The above code creates only two new objects, the StringBuffer and the final String that is returned. StringBuffer expands as needed, which is costly however, so it would be better to initilise the StringBuffer with the correct size from the start as shown.


    Note : Creation of extra Strings is not limited to operators’ (“+”) but
    there are several methods like concat(), trim(), substring(), and replace() in String Classes that generate new String instances. So use StringBuffer for computation intensive operations, which offer better performance.
     
  4. vbraojava

    vbraojava New Member

    Re: Explian Difference between String and StringBuffer ?

    Hi Premkumar,

    StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronised, which makes it slightly faster at the cost of not being thread-safe.
     
  5. What is the Difference b/w String and StringBuffer?

    A String literal is created at compile time and the compiler never creates a duplicate String literal. However, a string with the new operator is created at runtime and it always creates a new string even if it is identical to an already existing string. While the value of a string (the String object) cannot be changed, the value of a string reference can be changed; that is, you can assign another string to it.

    A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. String buffers are safe for use by multiple threads.

    A string created with the String class cannot be modified, whereas a string created with the StringBuffer class can be modified. The equals(…) method in the String class returns true if both strings are identical, while the
    equals(…) method in the StringBuffer class returns true only if both string references refer to the same string.
     
  6. Go through the replies Difference b/w StringBuffer and StringBuilder
     
  7. Konduruchaitanya

    Konduruchaitanya New Member

    There is one more difference Between String,StringBuffer class i.e., Strings doesnot contain any duplicates it means if we pass the same data for two strings then JVM will create only one reference whereas StringBuffer contains Duplicates.

    For Ex:

    class difference_between_string_and_stringbuffer_demo
    {
    public static void main(String[] ar)
    {
    String s="Hello";
    String s1="Hello";

    // Here JVM will creates only one reference number for above both(s and s1)

    StringBuffer s2=new StringBuffer("Hello");
    StringBuffer s3=new StringBuffer("Hello");

    // Here JVM will create two unique refernce nos for above both(s2 and s3)

    // we can see by using hashCode()

    System.out.println(s.hashCode()+"\n"+s1.hashCode()+"\n"+s2.hashCode()+"\n"+s3.hashCode());

    }
    }

    and second difference is Strings support for operator overloading whereas StringBuffer does not support operator overloading

    Example

    class operatooverloading
    {
    public static void main(String ar[])

    {
    String s="Hello";
    StringBuffer s1= "Chetanas";

    String s2=s+"a";
    StringBuffer s3=s1+"a";

    System.out.println(s2+"\n"+s3);
    }
    }

    In the above example There will raise one error at stringBuffer(i.e., incompatible types)

    and one more is
    if we want to print a string into reverse, reverse() is not available in String class. where is it available in StringBuffer
     
    Last edited: Jun 27, 2010
  8. zanaxp

    zanaxp New Member

    1. String is immutable vs. StringBuffer is mutable
    2. In String no method is synchronized vs. In StringBuffer every method is synchronized
    3. In String equals method is designed for content comparison vs. In StringBuffer it is designed for reference comparison.

    & for sake of java, i want to give some more additional information about these two.
    If content won't be changed frequently then we should go for String.
    If content will be changed frequently and if we require thread safety then go for StringBuffer.
    If content will be changed frequently and if we don't require thread safety then go for StringBuilder.
     

Share This Page