Home > DeveloperSection > Articles > String, String Builder and String Buffer in Java

String, String Builder and String Buffer in Java


Java Java 
Ratings:
0 Comment(s)
 714  View(s)
Rate this:

String, String Builder and String Buffer in Java

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

String text="Hello";

In string we can append character, concatenate string, find character, and remove character in java. String is a group of character or array of character. For example, the string "Hello" may be backed by the array ['H', 'e', 'l', 'l', 'o'] with offset 0 and length 5.

text=text.toUpperCase();

text=text.toLowerCase();

Output -:   HELLO       &         hello

Concatenation of String

String text1=="Hello ";

String text2=="World";

text1=text1+text2; or

text1+=text2;

Output-: Hello World;

StringBuffer a modifiable sequence of characters for use in creating strings. This class is intended as a direct replacement of StringBuffer for non-concurrent use; unlike StringBuffer this class is not synchronized.

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.

A String is immutable, i.e. when it's created, it can never change. 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.

StringBuffer buffer=new StringBuffer("Hello ");

buffer.append("World");

Output-: Hello World

StringBuilder java.lang.StringBuilder class is mutable sequence of characters. This provides an API compatible with StringBuffer, but with no guarantee of synchronization.

StringBuilder builder = new StringBuilder("Hello ");

builder.append("World");

Output-: Hello World

Difference between StringBuffer and StringBuilder

·         StringBuffer is synchronized, StringBuilder is not.

·         StringBuilder is faster than StringBuffer because it's not synchronized.

·         The StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. The StringBuilder class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

Here I am creating a sample of Reverse string using String and String butter

public class Demo {

     public static void main(String args[]) {

           String text;

           StringBuffer sbuffer;

           text = "Hello World";

           String demo = "";

// by unsing string

           for (int i = text.length() - 1; i >= 0; i--) {

                demo += text.charAt(i);

 

           }

// using StringBuffer

           sbuffer = new StringBuffer(text);

           System.out.println(demo);

           System.out.println(sbuffer.reverse());

 

     }

 

}

 

 

Output-: dlroW olleH

                dlroW olleH

 


Don't want to miss updates? Please click the below button!

Follow MindStick