← Back to Core Java Course | Chapter 6: Strings | Lesson 3 of 5

Java में StringBuilder और StringBuffer

StringBuilder एक text holder है जिसे आप हर बार एक नया बनाए बिना बार-बार बदल सकते हैं, एक printed page के बजाय एक eraser-friendly whiteboard जैसा। StringBuffer safe-for-many-users version है।
Syntax
java
StringBuilder builder = new StringBuilder();
builder.append(value);
String result = builder.toString();

Introduction to StringBuilder

StringBuilder characters का एक mutable sequence बनाता है जिसे जगह पर बदला जा सकता है, जो इसे एक loop के अंदर repeated String concatenation से नाटकीय रूप से तेज़ बनाता है — एक String पर हर += चुपचाप एक पूरी नई String object बनाता है, जबकि StringBuilder.append() बस एक internal buffer extend करता है।

उदाहरण: Introduction to StringBuilder

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder();
		sb.append("Hello");
		sb.append(" World");
		// Print a line to the console
		System.out.println(sb);
	}
}

Inserting and Deleting

चूंकि StringBuilder mutable है, आप insert(index, value) से किसी भी position पर characters insert कर सकते हैं या delete(start, end) से एक range हटा सकते हैं बिना हर बार एक नया object बनाए, एक immutable String पर equivalent operations के विपरीत।

उदाहरण: Inserting and Deleting

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder("Hello World");
		sb.insert(5, ",");
		// Print a line to the console
		System.out.println(sb);
		sb.delete(0, 6);
		// Print a line to the console
		System.out.println(sb);
	}
}

Reversing with StringBuilder

reverse() पूरी character sequence को जगह पर flip करता है, जो coding interview problems में एक आम building block है जैसे palindrome checks या digit-reversal puzzles — यह दोनों ends से manually characters swap करने से कहीं simpler है।

उदाहरण: Reversing with StringBuilder

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder("Hello");
		sb.reverse();
		// Print a line to the console
		System.out.println(sb);
	}
}

Checking Capacity and Length

length() report करता है कि अभी कितने characters stored हैं, जबकि capacity() report करता है कि builder को grow और reallocate होने से पहले कितनी internal buffer space allocated है — capacity हमेशा length से बड़ी या बराबर है, और आपको शायद ही कभी इसके बारे में सोचना चाहिए जब तक आप performance optimize न कर रहे हों।

उदाहरण: Checking Capacity and Length

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder("Hello");
		// Print a line to the console
		System.out.println(sb.length());
		// Print a line to the console
		System.out.println(sb.capacity());
	}
}

Thread Safe StringBuffer

StringBuffer का StringBuilder जैसा identical API है लेकिन इसके methods synchronize करता है, इसे कई threads में share करना extra locking overhead की कीमत पर safe बनाते हुए।

Single-threaded code के लिए default रूप से StringBuilder उपयोग करें, और सिर्फ तब StringBuffer के लिए पहुंचें जब आपको असल में वह thread safety चाहिए।

उदाहरण: Thread Safe StringBuffer

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuffer object and store it in sb
		StringBuffer sb = new StringBuffer();
		sb.append("Hello");
		sb.append(" World");
		// Print a line to the console
		System.out.println(sb);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक loop में StringBuilder के बजाय String += उपयोग करना, जो कई temporary objects बनाता है।
  2. StringBuilder objects को equals() से compare करना, जो references compare करता है text नहीं; toString() compare करें।
  3. जब एक String चाहिए हो तो toString() call करना भूल जाना।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.