String Vs String Buffer . Difference between String & StringBuffer class example

Java String object is immutable that means the content of the String Object contents cannot be modified after they are created . There are no methods available to manipulate the contents of the string once it is created. Methods that operate on a string returns a new string rather than manipulating the original copy of the string. String concatenation is done using "+" operator which creates many temporary objects and increases garbage collection . So String concatenation using "+" may be avoided.
The StringBuffer class represents a mutable string of characters that means their values can be changed after they are created. There are methods like append , insert are available which allows to manipulate the contents of the string once it is created . String concatenation is done using append method which avoids creating many temporary strings. So it is better to use the Java StringBuffer object to concatenate strings because it is more efficient.
Note : Both String and String buffers are safe for use by multiple threads because the operations on string will create a new string and String buffer methods are synchronized . Every string buffer has a initial capacity. If the internal buffer used by the string buffer overflows, it is automatically made larger.
String buffer / StringBuilder are used by the compiler to implement the binary string concatenation operator +. StringBuilder is similar to StringBuffer except String bulider is not synchronised. For example, the code:
input = "StringConcatenation is done using " + "StringBuffer.append " +4 + " u" is compiled to the equivalent to the following line.
input = new StringBuffer().append("StringConcatenation is done using ").append("StringBuffer.append ").append(4).append(" u").toString()
   
class StringCat
{
public static void main(String...args)
{
String input = "StringConcatenation is done using " + "StringBuffer.append " +4 + " u"
System.out.println("Output= "+ input);
}
}

Compile the above program using javac StringCat.java and Run the using javap -c StringCat which will display the bytecode of the StringCat.class as follows.
   
Compiled from "StringBuffer.java"
class StringBuffer extends java.lang.Object{
StringBuffer();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: ldc #2; //String StringConcatenation is done using StringBuffer.append 4 u
2: astore_1
3: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
6: new #4; //class java/lang/StringBuilder
9: dup
10: invokespecial #5; //Method java/lang/StringBuilder."<init>":()V
13: ldc #6; //String Output=
15: invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
18: aload_1
19: invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
22: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
25: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
28: return

}

0 comments:

Post a Comment

 
Copyright 2011 Java Online