Java Language StringBuffer String Buffer class

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Key Points :-

  • used to created mutable (modifiable) string.

  • Mutable :- Which can be changed.

  • is thread-safe i.e. multiple threads cannot access it simultaneously.

Methods :-

  • public synchronized StringBuffer append(String s)

  • public synchronized StringBuffer insert(int offset, String s)

  • public synchronized StringBuffer replace(int startIndex, int endIndex, String str)

  • public synchronized StringBuffer delete(int startIndex, int endIndex)

  • public synchronized StringBuffer reverse()

  • public int capacity()

  • public void ensureCapacity(int minimumCapacity)

  • public char charAt(int index)

  • public int length()

  • public String substring(int beginIndex)

  • public String substring(int beginIndex, int endIndex)

Example Showing diffrence between String and String Buffer implementation :-

class Test {
 public static void main(String args[])
 {
  String str = "study";
  str.concat("tonight");
  System.out.println(str);      // Output: study

  StringBuffer strB = new StringBuffer("study");
  strB.append("tonight");
  System.out.println(strB);    // Output: studytonight
 }
}


Got any Java Language Question?