Skip to content

A simple Java project demonstrating the differences between String, StringBuilder, and StringBuffer — covering immutability, mutability, synchronization, and performance.

Notifications You must be signed in to change notification settings

Jayeshsarvade/String-StringBuilder-StringBuffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

String-StringBuilder-StringBuffer

A simple Java project demonstrating the differences between String, StringBuilder, and StringBuffer — covering immutability, mutability, synchronization, and performance.

📌 String

  • Immutable Class → Once a String object is created, it cannot be changed.
  • Every modification creates a new object in the String pool.
  • Suitable when the data is not frequently modified.
  • Thread-Safe because it is immutable.

Example:

String s = "Hello"; s.concat(" World"); System.out.println(s); // Output: Hello (not Hello World)

##📌 StringBuilder

  • Mutable Class → Can be modified without creating new objects.
  • Introduced in Java 1.5.
  • Not synchronized → Better performance in single-threaded environments.
  • Faster than both String and StringBuffer.

Example:

StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb); // Output: Hello World

###📌 StringBuffer

  • Mutable Class → Can be modified without creating new objects.
  • Synchronized → Thread-safe but slower than StringBuilder.
  • Used in multi-threaded environments when thread safety is required.

Example:

StringBuffer sbf = new StringBuffer("Hello"); sbf.append(" World"); System.out.println(sbf); // Output: Hello World

Feature String (Immutable) StringBuilder (Mutable) StringBuffer (Mutable)
Mutability ❌ Immutable ✅ Mutable ✅ Mutable
Thread-Safe ✅ Yes ❌ No ✅ Yes
Performance ❌ Slow ✅ Fastest ⚠️ Slower than StringBuilder
Use Case Fixed data, keys Single-threaded apps Multi-threaded apps

🚀 Conclusion

  • Use String when values don’t change often.
  • Use StringBuilder for single-threaded applications with lots of modifications.
  • Use StringBuffer for multi-threaded applications where thread safety is required.

About

A simple Java project demonstrating the differences between String, StringBuilder, and StringBuffer — covering immutability, mutability, synchronization, and performance.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published