📘 Lesson  ·  Lesson 37

String vs StringBuffer vs StringBuilder

String vs StringBuffer vs StringBuilder

Three String Types

String is immutable (cannot change). StringBuffer and StringBuilder are mutable (can change) — better for many edits.

Comparison

TypeMutableThread-safeSpeed
StringNo--
StringBufferYesYesslower
StringBuilderYesNofastest

Example

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");   // modifies same object
System.out.println(sb);   // Hello World
Hello World

Summary

  • String = immutable; StringBuffer = mutable + thread-safe; StringBuilder = mutable + fastest.
  • Use StringBuilder for many string edits in single-threaded code.

तीन String Types

String immutable है (बदल नहीं सकते)। StringBuffer और StringBuilder mutable हैं (बदल सकते हैं) — बहुत edits के लिए बेहतर।

तुलना

TypeMutableThread-safeSpeed
Stringनहीं--
StringBufferहाँहाँधीमा
StringBuilderहाँनहींसबसे तेज़

Example

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");   // same object modify करता है
System.out.println(sb);   // Hello World
Hello World

सारांश

  • String = immutable; StringBuffer = mutable + thread-safe; StringBuilder = mutable + सबसे तेज़।
  • Single-threaded code में बहुत string edits को StringBuilder use करें।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n