Day 19: StringBuffer and StringBuilder in Java:

Mutable String: A String that can be modified or changed is known as mutable String.

StringBuffer and StringBuilder are classes in Java used for creating and manipulating mutable strings. Unlike String, which is immutable, both StringBuffer and StringBuilder allow modifications to the content of strings without creating new objects.

StringBuffer

  1. Thread Safety: All methods in StringBuffer are synchronized, making it thread-safe for multi-threaded environments.
  2. Performance: Slightly slower compared to StringBuilder due to the overhead of synchronization.

Example: Using StringBuffer


StringBuilder

  1. Thread Safety: StringBuilder is not synchronized, meaning it is not thread-safe.
  2. Performance: Faster than StringBuffer in single-threaded environments due to the absence of synchronization.

Example: Using StringBuilder


When to Use

  • Use StringBuffer when working in a multi-threaded environment where multiple threads may access and modify the same string.
  • Use StringBuilder in a single-threaded environment for better performance, as it avoids the overhead of synchronization.

Methods Common to Both

  • append(String str): Adds the specified string to the end.
  • insert(int offset, String str): Inserts the string at the specified position.
  • replace(int start, int end, String str): Replaces content within a specified range.
  • delete(int start, int end): Removes content within a specified range.
  • reverse(): Reverses the string.
  • capacity(): Returns the current capacity of the buffer.
  • ensureCapacity(int minimumCapacity): Ensures that the capacity is at least equal to the specified value.


So when to use what?

  • Use String when immutability is needed.
  • Use StringBuffer for thread-safe, mutable strings.
  • Use StringBuilder for mutable strings in a single-threaded environment for better performance.

Happy Learning :)

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了