StringBuffer vs String

View previous topic View next topic Go down

StringBuffer vs String

Post by BIT0122-Amit on Fri Sep 04, 2009 11:46 pm

Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.

The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. In String manipulation code, character strings are routinely concatenated.

Using the String class, concatenations are typically performed as follows:

Code:
    String str = new String ("Stanford  ");
    str += "Lost!!";


If you were to use StringBuffer to perform the same concatenation, you would need code that looks like this:

Code:
    StringBuffer str = new StringBuffer ("Stanford ");
    str.append("Lost!!");


Developers usually assume that the first example above is more efficient because they think that the second example, which uses the append method for concatenation, is more costly than the first example, which uses the + operator to concatenate two String objects.

The + operator appears innocent, but the code generated produces some surprises. Using a StringBuffer for concatenation can in fact produce code that is significantly faster than using a String. To discover why this is the case, we must examine the generated bytecode from our two examples.


By Reggie Hutcherson
Source

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: StringBuffer vs String

Post by BIT0112-Rokon on Mon Sep 07, 2009 9:25 am

this is an important post..
I've used string and stirngbuffer in my calculator's code!
in this two class, String and StringBuffer, the String class is used to store and manipulate character strings that cannot be changed. Another way of describing this is to say that Strings are read only and immutable. The StringBuffer class, on the other hand, is used to represent characters that can be modified.

The significant difference between these two classes is performance where StringBuffer is faster than String when performing simple concatenations. When a String is being manipulated in code, character string are routinely concatenated like shown in the following example:

String name = new String("Alex");
name += ", Hunter";


Here is a example:
Code:

public class StringBufferDemo {

    public static void main(String[] args) {

        StringBuffer name = new StringBuffer("Alex");
        name.append(", Hunter");

        // One way to convert a StringBuffer to a String
        String nameStr1 = name.toString();

        // Allocates a new string that contains the sequence of characters
        // currently contained in the string buffer argument.
        String nameStr2 = new String(name);

        System.out.println("name      : " + name);
        System.out.println("nameStr1  : " + nameStr1);
        System.out.println("nameStr2  : " + nameStr2);
     
    }

}

ok best of luck
bazlur rahman
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum