String VS StringBuffer VS StringBuilder
String is immutable that is if you want to change a string value another object gets created. On the other hand StringBuffer and StringBuilder are mutable they can change their values.
More about String :
Generally, an object that represents sequence of char values known as String. An array of characters works same as string in Java language. For instance:
-
- char[] charactersType ={‘h’,‘a’,‘r’,‘u’,‘n’};
- String stringType =new String(charactersType );
Equivalent to below statements,
- String stringType=“String Same”;
So when to use String?
Java String class or java.lang.String provides class implements Serializable, Comparable etc interfaces. The Java String is immutable. Therefore it cannot be changed. Whenever want to change any string, a new instance is created everytime. For mutable strings, you can use StringBuffer and StringBuilder classes. If you use string that is not going to change values use a string class then.
More about StringBuffer
StringBuffer is thread safe. mutable and synchronous which is like a String, but can be modified. Certain method calls can be changed length and contents of chartecters. One of the major feature is can handle multiple thread. StringBuffer implements class and object below.
public final class StringBuffer
extends Object
implements Serializable, CharSequence
For multiple threads, synchronized is necessary operations. Two principal StringBuffer activities are the ‘ append’ and ‘insert’ methods, which are overloaded and need to accept data of any type.
For example,
If ‘p defines to a string buffer object whose current character sequence are “Harun”, then the method call p.append(“or”) would cause the string buffer to contain “Harunor”. On the other hand, if p.insert(4, “ni”) would alter the string buffer to contain “harunin”.
when to use StringBuffer?
If your string can change and will be accessed by a multiple thread then use StringBuffer. Because it can deal with multiple threads, synchronizations necessary operations.
More about StringBuilder
StringBuilder is used for multiple thread.It also can represent a mutable sequence of characters which is an alternative to String Class. Very much similar to StringBuffer class. Though StringBuffer class synchronizations are guaranteed, where StringBuilder provides no guarantee of synchronization. Additionally can not smoothly deal multiple thread. Here is the snippet of StringBuilder extandable object and classes that can be implements.
public final class StringBuilder
extends Object
implements Serializable, CharSequence
Constructions and method with StringBuilder overview
StringBuilder(): This constructs with no characters in it and an initial capacity of 16 characters.
StringBuilder(CharSequence seq):This constructs same characters as the specified CharSequence.
StringBuilder(String str): This constructs a string builder initialized to the contents of the specified string.
when to use StringBuilder?
If your string can change lots of logic and operation in construction of the string and only be accessed by a single thread.
Three of topic implements on java given below:
/*Java program toimplement String VS StringBuffer VS StringBuilder */
class draftsbook
{
/* String Concatenation */
public static void cut1(String sString)
{
sString = sString + “harunorr”;
}
/*StringBuilder Concatenation*/
public static void cut2(StringBuilder sStringBuilder)
{
sStringBuilder.append(“harunorr”);
}
/* StringBuffer Concatenation*/
public static void cut3(StringBuffer sStringBuffer )
{
sStringBuffer .append(“harunorr”);
}
public static void main(String[] args)
{
String sString_1 = “jees”;
/* sString_1 is not changed as different refence object*/
cut1(sString_1 );
System.out.println(“String: ” + sString_1 );
StringBuilder sString_2 = new StringBuilder(“jees”);
/* sString_2 changed */
cut2(sString_2);
System.out.println(“StringBuilder: ” + sString_2);
StringBuffer sString_3 = new StringBuffer(“jees”);
/* sString_3 got changed */
cut3(sString_3);
System.out.println(“StringBuffer: ” + sString_3);
}
}
OUTPUT
String: jees
StringBuilder: jeesharunorr
StringBuffer: jeesharunorr
See more Topic on Java OOP with real-time example
Nested class in JAVA with EXAMPLES
What is the defference between String, StringBuffer and StringBuilder.
Java Interface tutorials with Example.