String Operations, StringBuilder and StringBuffer:
String is a non-primitive type in java. Strings are considered as Immutable. Before proceeding to example, Let us understand about String, StringBuilder and StringBuffer.

String is Immutable.

What is meant by Immutable?
In Java, Strings are treated as Objects. Immutable means once the object has been created it cannot be changed.

What are the advantages of String being Immutable?

  • It helps in Multi-threading, We can be sure that no one will change the string
  • Reduction in Memory usage

Two ways of Creating Strings:

  • String s1 = “StringValue”;
  • String S2 = new String(“StringValue2”);

Difference Between StringBuilder and StringBuffer:

StringBuilder StringBuffer
StringBuilder is mutable StringBuffer is mutable
StringBuilder is unsynchronized StringBuffer is Synchronized
StringBuilder is faster StringBuffer is slower when compared to StringBuilder

NOTE: StringBuffer and StringBuilder does not Override equals() and hashcode()

When to use String, StringBuffer and StringBuilder?
If our string is not going change, We use String.
If our string can change and accessed only from single thread, We can use StringBuilder.
If our string can change and can be accessed from multiple threads, We can use StringBuffer.

Example,

public class StringOp {
    
    public static void main(String args[]){
        
        String s1 = "String1";
        String s2="String2";
        String s3="String1";
        String s4 = new String("Object Check");
        System.out.println("Char at: "+s1.charAt(2));
        System.out.println("Strings added with + :"+s1+s2);
        System.out.println("Concatenation Function: "+s1.concat(s2));
        System.out.println("Compares to: S1 and S2:  "+s1.compareTo(s2));
        System.out.println("Compares to: S1 and S2:  "+s1.compareToIgnoreCase(s2));
        System.out.println("Compares to: S1 and S3:  "+s1.compareTo(s3));
        System.out.println("Compares to: S1 and S3:  "+s1.compareToIgnoreCase(s3));
        System.out.println("Contains : "+s1.contains("ring"));
        System.out.println("Ends with: "+s4.endsWith("check"));
        System.out.println("Hashcode of: "+s1.hashCode());
        System.out.println("Canonical Representation: "+s1.intern());
        System.out.println("LastIndexOf Representation: "+s1.lastIndexOf("n"));
        System.out.println("Replace: "+s1.replace("tri", "itr"));
        System.out.println("Replace First: "+s1.replaceFirst("itr", "TRI"));
        System.out.println("Replace All: "+s1.replaceAll("i", "j"));
        String s5[] = s4.split(" ");
        System.out.println("split Spring: "+s5[0] +" splited "+s5[1]);
        char c[] = s2.toCharArray();
        System.out.println("To Char Array: c[0] and c[3] printed: "+c[0]+c[3]);
        System.out.println("ToUpperCase: "+s2.toUpperCase()+" LowerCase: "+s2.toLowerCase());
        String s6 = " Blank Space at Front and End ";
        System.out.println("Trim - Removing Blank Spaces: "+s6.trim());
        s1 = s1.replace("t", "3");
        System.out.println(s1);
        
        
        
        
        System.out.print("-------------------------String Buffer-----------------------------------");
        
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer stringBuffer1 = new StringBuffer();
        stringBuffer.append("SampleString");
        stringBuffer1.append("SampleString");
        System.out.println();
        System.out.println("Character at Index 3: "+stringBuffer.charAt(3)); 
        //System.out.println("Reversing String: "+stringBuffer.reverse());
        System.out.println("Replace String: "+stringBuffer.replace(3, 6, "REPLS"));
        System.out.println("StringBuffer Capacity: "+stringBuffer.capacity());
        System.out.println("Deleting Char: "+stringBuffer.deleteCharAt(3)); //start and end
        System.out.println("Inserting: "+stringBuffer.insert(3, "J"));
        System.out.println("Last Index of: "+stringBuffer.lastIndexOf("ring"));
        System.out.println("Substring of: "+stringBuffer.substring(5));
        System.out.println("StringBuffer Compare: "+stringBuffer.equals(stringBuffer1));
        
        // String Builder
        
        System.out.println("--------------------------String Builder------------------------------------------");
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder stringBuilder1 = new StringBuilder();
        
        stringBuilder.append("SampleString");
        stringBuilder1.append("SampleString");
        System.out.println(stringBuilder);
        System.out.println("Character at Index 3: "+stringBuilder.charAt(3)); 
        //System.out.println("Reversing String: "+stringBuffer.reverse());
        System.out.println("Replace String: "+stringBuilder.replace(3, 6, "REPLS"));
        System.out.println("StringBuffer Capacity: "+stringBuilder.capacity());
        System.out.println("Deleting Char: "+stringBuilder.deleteCharAt(3)); //start and end
        System.out.println("Inserting: "+stringBuilder.insert(3, "J"));
        System.out.println("Last Index of: "+stringBuilder.lastIndexOf("ring"));
        System.out.println("Substring of: "+stringBuilder.substring(5));
        System.out.println("Equals: "+stringBuilder.equals(stringBuilder1)); // False because string builder does not override equals
    }
}

Output:
op1
op2

 

By Sri

One thought on “String Operations, StringBuilder and StringBuffer”

Leave a Reply

Your email address will not be published. Required fields are marked *