Object Oriented Programming with Java

⌘K
  1. Home
  2. Docs
  3. Object Oriented Programmi...
  4. Essential Java Classes
  5. String, StringBuffer, and StringBuilder Classes

String, StringBuffer, and StringBuilder Classes

String, StringBuffer, and StringBuilder Classes:

  • These three classes are fundamental for handling sequences of characters (text) in Java.

It represents immutable character sequences. Once a String is created, its content cannot be changed.

  • Any operation that appears to modify a String (e.g., concatenation) actually creates a new String object.
  • It is ideal for storing text that doesn’t need frequent modification, such as names, fixed messages, and constant values.
  • Generally efficient for simple string manipulations, but can be inefficient for extensive modifications within loops due to the creation of many intermediate String objects.

Example:

public class StringExample {
    public static void main(String[] args) {
    
      String name = "Java";
      name = name + " Programming"; // Creates a new String object
      
      // Printing the new value after concatenation
        System.out.println("New String: " + name);
      
     }
}

It represents mutable character sequences, meaning their content can be changed after creation.

  • It is thread-safe (synchronized). This means that multiple threads can access and modify a StringBuffer instance concurrently without corrupting its state.
  • It is suitable for building strings in multi-threaded environments where thread-safety is crucial, or when you need to perform many modifications to a string.

Example:

public class StringBufferExample {
    public static void main(String[] args) {
        // Creating a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello");

        // Appending text to the existing buffer
        sb.append(" World");

        // Printing the final content
        System.out.println("Final String: " + sb);
    }
}

It also represents mutable character sequences like StringBuffer, meaning their content can be changed after creation.

  • It is not thread-safe (unsynchronized). This makes it faster than StringBuffer because it doesn’t incur the overhead of synchronization.
  • It is preferred for building strings in single-threaded environments or when thread-safety is handled externally.
  • It’s the most common choice for general-purpose string manipulation where performance is a concern.

Example:

public class StringBuilderExample {
    public static void main(String[] args) {
        // Creating a StringBuilder object with initial content
        StringBuilder sb = new StringBuilder("Hello");

        // Appending text to the existing builder
        sb.append(" Java");

        // Printing the final content
        System.out.println("Final String: " + sb);
    }
}
Difference between String Class, StringBuffer Class and StringBuilder Class
Tags , ,

How can we help?

Leave a Reply

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