
String Buffer/String Builder in Java
What is String Buffer: StringBuffer is a mutable and thread-safe class used to modify strings. String Buffer is a child of the CharSequence interface StringBuffer is a **mutable class **in Java. When we perform operations like append or modify, it updates the same object instead of creating a new one, which improves performance. StringBuffer is synchronised/THREAD-SAFE (Multiple threads can't access it simultaneously) How to create a StringBuffer Object: By using only new Keyword StringBuffer s = new StringBuffer ( "Java" ); Object will be created in heap memory, and the reference variable is pointing to it. StringBuffer Methods: append() → Add data Adds text at the end StringBuffer sb = new StringBuffer ( "Java" ); sb . append ( " World" ); System . out . println ( sb ); O / P: Java World insert() → Insert at specific position Adds text at a given index StringBuffer sb = new StringBuffer ( "Java" ); sb . insert ( 2 , "XX" ); System . out . println ( sb ); o / P: JaXXva replace() → Rep
Continue reading on Dev.to Tutorial
Opens in a new tab



