STRINGBUFFER: ============= ==> it is another class for the string handling from "java.lang" package. About Strings: ============== 1) Group of characters which must be enclosed with double quotes. Ex: "String" 2) Strings are immutable ex: toLowerCase() s1 = s1.toLowerCase() 3) Thread safe objects What is StringBuffer: ====================== ==> it is one of the built-in class ==> like the strings: stringbuffer is the group of characters which must be enclosed with double quotes. ==> StringBuffer ==> Mutable object ==> Thread safe objects ===================================== How to Create the stringbuffer? =============================== String s1 = "Hello"; // string literal==> SCP String s2 = new String("Hello"); // string object ==> Heap memory 1) StringBuffer never allow to define in literal format. 2) StringBuffer can always allow to define in object format only. Syntax: StringBuffer object-name = new StringBuffer("Value"); class StringBufferDefinition{ public static void main(String[] args) { // StringBuffer sb = "Hello All"; StringBuffer sb = new StringBuffer("Hello"); System.out.println("The Given String Buffer Object value = "+sb); } } ======================================================= How the stringbuffer is mutable? ================================ append(): ======== adding the stringbuffer-content at the end of another stringbuffer-content ex: "Hello" append(" World") ==> Hello World Syntax: stringBufferObject.append("value"); class StringBufferDefinition{ public static void main(String[] args) { StringBuffer s1 = new StringBuffer("Java "); StringBuffer s2 = new StringBuffer("Programming "); StringBuffer s3 = new StringBuffer("Language"); System.out.println("The Given String Buffers are = "); System.out.println(s1); System.out.println(s2); System.out.println(s3); s1.append(s2); System.out.println("The Given String Buffers after the modification are = "); System.out.println(s1); System.out.println(s2); System.out.println(s3); s1.append(s3); System.out.println("The Given String Buffers are = "); System.out.println(s1); System.out.println(s2); System.out.println(s3); } } Note: ===== We can append any value to stringbuffer using append() class StringBufferAppendOperation{ public static void main(String[] args) { StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb); sb.append(123); System.out.println(sb); sb.append(12.23f); System.out.println(sb); sb.append(true); System.out.println(sb); } } ========================================== substring(): =========== ==> to access the part of the content between specified index, subString() can be used. Syntax: stringbuffer-object.substring(start-index, end-index) Note: ===== subString() can return a string. class SubStringMethod{ public static void main(String[] args) { StringBuffer sb = new StringBuffer("Java Programming Language"); String s1 = sb.substring(10); String s2 = sb.substring(10,20); System.out.println(sb); System.out.println(s1); System.out.println(s2); } } ============================================================= insert(): ========= => can add the content in the stringbuffer content at the given index position. Syntax: stringbuffer-object.insert(index, conent); class InsertionOfData{ public static void main(String[] args) { StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb); sb.insert(2,'L'); System.out.println(sb); sb.insert(3,"java"); System.out.println(sb); } } ==================================================== length(): ======== ==> return the total number of characters from the given stringbuffer. Syntax: sb.length() capacity(): =========== ==> return the volume (total space in the memory) of the stringbuffer Syntax: sb.capacity() class LengthVsCapacity{ public static void main(String[] args) { StringBuffer sv = new StringBuffer("Hello"); System.out.println("The Length = "+ sv.length()); System.out.println("The Capacity = "+sv.capacity()); sv.append(" world!"); System.out.println("The Length = "+ sv.length()); System.out.println("The Capacity = "+sv.capacity()); sv.append("123456789"); System.out.println("The Length = "+ sv.length()); System.out.println("The Capacity = "+sv.capacity()); sv.insert(10,"0"); System.out.println("The Length = "+ sv.length()); System.out.println("The Capacity = "+sv.capacity()); sv.append("12345678901234567890135"); System.out.println("The Length = "+ sv.length()); System.out.println("The Capacity = "+sv.capacity()); } } Note: ===== Length ==> number of characters capacity ==> length + 16-spaces while modification: length < capacity length ===> number of characters capacity ==> o-length + n-length + spaces length > capacity length ==> number of characters capacity ==> length * 2 ========================================