Java String Handling Tutorial

On this page, you will find all the tutorials, guides, and examples on Java String, StringBuilder, and StringBuffer.

In Java, String, StringBuilder, and StringBuffer are used to represent and manipulate sequences of characters. 


Immutable: Once a String object is created, its value cannot be changed. Any modification to the string results in a new object. 

Syntax: Strings can be created using string literals or the new keyword. 

Performance: Since strings are immutable, repeated modifications can be less efficient as new objects are created for each change. 

Thread Safety: Immutable objects are inherently thread-safe, so no synchronization is required.

String str = "Hello, World!";


Mutable: Unlike String, StringBuilder is mutable, meaning that its content can be changed without creating a new object. 

Syntax: StringBuilder objects must be explicitly created using the new keyword.

Performance: Since it's mutable, StringBuilder is generally more efficient for repeated modifications to the same object. 

Thread Safety: It is not synchronized, making it not thread-safe. It's suitable for single-threaded scenarios.


StringBuilder builder = new StringBuilder("Hello");
builder.append(", World!");


Mutable: Like StringBuilder, StringBuffer is also mutable. 

Syntax: StringBuffer objects must be explicitly created using the new keyword. 

Performance: Slightly slower than StringBuilder due to synchronization. 

Thread Safety: It is synchronized, making it thread-safe. If you need to manipulate strings across multiple threads, StringBuffer might be the preferred option. 

StringBuffer buffer = new StringBuffer("Hello");
buffer.append(", World!");

Java String Blog Posts, Tutorials, and Examples

  1. Java String: A Guide to String Basics, Methods, Immutability, Performance, and Best Practices
  2. Java String Class API Guide - Covers all the String Methods
  3. When to Use String, StringBuffer, and StringBuilder in Java
  4. String vs StringBuilder vs StringBuffer in Java
  5. Best Way to Reverse a String in Java
  6. Guide to Java String Constant Pool
  7. Guide to String Best Practices in Java (Best Practice) 
  8. String Special Operations with Examples
  9. String Comparison Methods with Examples
  10. String Methods for Character Extraction
  11. String Searching Methods with Examples
  12. String Modifying Methods with Examples
  13. Java 8 StringJoiner Class

Java StringBuffer Blog Posts, Tutorials, and Examples

Interview Preparation: Java String Programs with Output

These are the frequently asked Java programs in the interviews:

Java String Conversion Examples

Conversion from String to Wrapper classes or Primitive types