- String is immutable objects means once created it cannot be changed, reference will point to new object.
- If our application has string manipulation activity, then there will be many discarded string object in heap memory which might result in performance impact.
- StringBuffer/StringBuilder is mutable
- StringBuffer/StringBuilder can have characters/strings may be inserted in between or appended at end for which the StringBuffer/StringBuilder automatically grow
Difference between StringBuffer & StringBuilder
- Methods in StringBuilder are not synchronized hence better performance over StringBuffer
- Methods in StringBuilder are not synchronized hence it is not thread safe
Examples with basic methods
String
public class StringDemo {
public static void main (String[] args)
{
// Following are 2 ways to instantiate String object
String s=new String("ABCDMNOP");
String s1="PqRS";
Date e=new Date();
System.out.println("Current time: " + new Timestamp(e.getTime()));
System.out.println(s); // Print ABCDMNOP
System.out.println(s1); // Print PqRS
System.out.println(s.length());
System.out.println(s.toLowerCase());
System.out.println(s.charAt(1));
System.out.println(s1.charAt(3));
//System.out.println(s1.charAt(4)); // Throws IndexOutOfBoundsException since length of s1 is 4
System.out.println(s.substring(1)); // Return the substring starting from index 1
System.out.println(s.substring(1,4)); /* Return the substring starting from index 1
upto (endIndex-1) index i.e. BCD*/
System.out.println(s.subSequence(1, 4)); /* works similar to substring except the difference that return type of this method is CharSequence while return type for substring is String */
}
}
StringBuffer
public class StringBufferDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=new String("ABCDMNOP");
StringBuffer sb=new StringBuffer(s); /* StringBuffer works same as StringBuilder with a difference that methods in StringBuffer are synchronized while methods in StringBuilder are non-synchronized hence StringBuilder is not thread safe so better performance */
System.out.println(sb);
System.out.println(sb.deleteCharAt(2));
System.out.println(sb.delete(4, 6)); /* delete the substring starting from startIndex upto (endIndex-1) index i.e. NO */
System.out.println(sb.replace(1, 3, "XYZQ")); /* replace the substring starting from index 1 upto (endIndex-1) index with the specified string pattern i.e. BD with "XYZQ" */
System.out.println(sb.reverse());
System.out.println(sb);//Original String is also changed with the last operation
System.out.println(sb.charAt(3));
System.out.println(sb.substring(4, 7)); /* Return the substring starting from index 1 upto (endIndex-1) index i.e. YXA*/
System.out.println(sb.insert(2, 'x'));/* Insert the specified character on given index i.e. x on index 2 */
System.out.println(sb.insert(3, true)); /* Insert the specified boolean on given index i.e. x on index 3 */
System.out.println(sb.insert(3, "Test")); /* Insert the specified string on given index i.e. x on index 3 */
}
}
StringBuilder
public class StringBuilderDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=new String("ABCDMNOP");
StringBuilder sb=new StringBuilder(s);
System.out.println(sb);
System.out.println(sb.deleteCharAt(2));
System.out.println(sb.delete(4, 6)); /* delete the substring starting from startIndex upto (endIndex-1) index i.e. NO */
System.out.println(sb.replace(1, 3, "XYZQ")); /* replace the substring starting from index 1 upto (endIndex-1) index with the specified string pattern i.e. BD with "XYZQ" */
System.out.println(sb.reverse());
System.out.println(sb);//Original String is also changed with the last operation
System.out.println(sb.charAt(3));
System.out.println(sb.substring(4, 7)); /* Return the substring starting from index 1 upto (endIndex-1) index i.e. YXA*/
System.out.println(sb.insert(2, 'x'));/* Insert the specified character on given index i.e. x on index 2 */
System.out.println(sb.insert(3, true)); /* Insert the specified boolean on given index i.e. x on index 3 */
System.out.println(sb.insert(3, "Test")); /* Insert the specified string on given index i.e. x on index 3 */
}
}