Skip to content
Discussion options

You must be logged in to vote

✅ Difference Between String, StringBuilder, and StringBuffer in Java

Feature String StringBuilder StringBuffer
Mutability Immutable – cannot be changed Mutable – can be changed Mutable – can be changed
Thread Safety Not thread-safe Not thread-safe Thread-safe (synchronized methods)
Performance Slow for modifications Faster than StringBuffer Slower than StringBuilder
Synchronization No No Yes
Use Case Use when string doesn’t change Use in single-threaded environments Use in multi-threaded environments
Package java.lang.String java.lang.StringBuilder java.lang.StringBuffer
Introduced In Java 1.0 Java 1.5 Java 1.0

🧠 Example:

String (Immutable)

String s = "Hello";
s.…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by AmjustGettingStarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants