StringBuilder Class: Mutable, Non-Synchronized and High Performance Notes in Hindi & English : StringBuilder क्लास: नॉन-सिंक्रोनाइज़्ड, तेज़ और परिवर्तनीय और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम StringBuilder Class: Mutable, Non-Synchronized and High Performance (StringBuilder क्लास: नॉन-सिंक्रोनाइज़्ड, तेज़ और परिवर्तनीय) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The StringBuilder class in java.lang provides a mutable, non-synchronized character sequence designed for high-performance string manipulation in single-threaded applications. Introduced in JDK 1.5, it modifies its internal buffer in place and dynamically expands capacity without creating intermediate String objects.

अध्याय सार: java.lang पैकेज में स्थित StringBuilder क्लास एक परिवर्तनीय (mutable) और नॉन-सिंक्रोनाइज़्ड कैरेक्टर अनुक्रम प्रदान करती है, जिसे सिंगल-थ्रेडेड प्रोग्राम में उच्च प्रदर्शन के लिए डिज़ाइन किया गया है। JDK 1.5 में जोड़ी गई यह क्लास नए String ऑब्जेक्ट बनाए बिना अपने आंतरिक बफर में ही बदलाव करती है और आवश्यकतानुसार क्षमता बढ़ाती है।

StringBuilder Class: Mutable, Non-Synchronized and High Performance: Detailed Notes & Architecture (StringBuilder क्लास: नॉन-सिंक्रोनाइज़्ड, तेज़ और परिवर्तनीय)

1. Architecture and Internal Buffer Storage (आर्किटेक्चर और आंतरिक बफर स्टोरेज)

[EN] StringBuilder is declared as public final class StringBuilder extending AbstractStringBuilder and implementing Serializable, CharSequence, Appendable, and Comparable<StringBuilder> (added in Java 11). From JDK 5 through JDK 8, it backed text using a UTF-16 char[] array consuming 2 bytes per character. Since JDK 9 (JEP 254 Compact Strings), it stores characters inside a byte[] array paired with a coder byte (LATIN1 = 0 for 1-byte ASCII/ISO-8859-1 or UTF16 = 1 for 2-byte characters), cutting Heap footprint nearly in half for Latin-1 text.

[HI] StringBuilder को public final class StringBuilder के रूप में घोषित किया गया है जो AbstractStringBuilder को इनहेरिट करती है तथा Serializable, CharSequence, Appendable और Java 11 से Comparable<StringBuilder> को लागू करती है। JDK 5 से JDK 8 तक यह UTF-16 char[] ऐरे (प्रति अक्षर 2 बाइट) का उपयोग करती थी। JDK 9 (JEP 254 Compact Strings) के बाद से यह एक byte[] ऐरे और coder बाइट (LATIN1 = 0 या UTF16 = 1) का उपयोग करती है, जिससे सामान्य ASCII टेक्स्ट की हीप मेमोरी लगभग आधी हो जाती है।

2. Constructors, Initial Capacity, and Expansion Formula (कंस्ट्रक्टर्स, प्रारंभिक क्षमता और विस्तार सूत्र)

[EN] The no-argument constructor new StringBuilder() allocates an internal buffer with a default capacity of 16 characters. Calling new StringBuilder(String str) initializes capacity to str.length() + 16, whereas new StringBuilder(int capacity) sets an exact initial capacity and throws NegativeArraySizeException if negative. Whenever an append or insert exceeds the current capacity, ensureCapacity allocates a new buffer of size (oldCapacity * 2) + 2 (or exact minimumCapacity if larger) and copies existing elements via Arrays.copyOf().

[HI] बिना पैरामीटर वाला कंस्ट्रक्टर new StringBuilder() डिफ़ॉल्ट रूप से 16 कैरेक्टर क्षमता वाला बफर बनाता है। new StringBuilder(String str) कुल str.length() + 16 क्षमता आरक्षित करता है, जबकि new StringBuilder(int capacity) निश्चित क्षमता देता है और ऋणात्मक मान पर NegativeArraySizeException फेंकता है। जब डेटा वर्तमान क्षमता से अधिक हो जाता है, तो नया आकार (oldCapacity * 2) + 2 (या आवश्यक minimumCapacity, जो बड़ा हो) के सूत्र से बढ़ता है।

3. Core Mutator Methods and Fluent Method Chaining (मुख्य परिवर्तनीय मेथड्स और फ्लुएंट मेथड चेनिंग)

[EN] Mutating methods including append(), insert(), replace(), delete(), deleteCharAt(), and reverse() modify the underlying array in place and return a reference to this, enabling fluent method chaining in a single expression. Notice that delete(start, end) and replace(start, end, str) treat start as inclusive and end as exclusive, throwing StringIndexOutOfBoundsException on invalid indices. Meanwhile, substring() and toString() return a new String without mutating the StringBuilder, and setLength(newLength) truncates or pads with null characters ('\u0000').

[HI] append(), insert(), replace(), delete(), deleteCharAt(), और reverse() जैसे मेथड्स आंतरिक ऐरे को सीधे बदलते हैं और 'this' रेफरेंस लौटाते हैं, जिससे एक ही पंक्ति में मेथड चेनिंग संभव होती है। delete(start, end) और replace(start, end, str) में start शामिल (inclusive) और end बाहर (exclusive) होता है तथा गलत इंडेक्स पर StringIndexOutOfBoundsException आता है। इसके विपरीत substring() और toString() बफर को बदले बिना नया String बनाते हैं, और setLength() टेक्स्ट को छोटा करता है या '\u0000' नल कैरेक्टर जोड़ता है।

4. Non-Synchronized Performance and the equals() Pitfall (नॉन-सिंक्रोनाइज़्ड प्रदर्शन और equals() से जुड़ी सावधानी)

[EN] Unlike StringBuffer, none of StringBuilder's public methods carry the synchronized modifier, eliminating intrinsic monitor lock acquisition and release overhead in single-threaded code and loops. However, sharing one StringBuilder across concurrent threads without external locking causes data corruption or ArrayIndexOutOfBoundsException. Critically, StringBuilder does NOT override Object.equals() or Object.hashCode(); calling sb1.equals(sb2) compares Heap references (==) rather than text content, so convert with toString().equals() or use compareTo() == 0 (Java 11+).

[HI] StringBuffer के विपरीत, StringBuilder के किसी भी पब्लिक मेथड में synchronized कीवर्ड नहीं होता, जिससे सिंगल-थ्रेड कोड और लूप में मॉनिटर लॉक का अतिरिक्त समय बचता है। हालांकि, बिना बाहरी सिंक्रोनाइज़ेशन के इसे मल्टी-थ्रेडिंग में साझा करने से डेटा खराब हो सकता है। एक महत्वपूर्ण परीक्षा तथ्य यह है कि StringBuilder, Object.equals() और hashCode() को ओवरराइड नहीं करता; इसलिए sb1.equals(sb2) कंटेंट की बजाय मेमोरी रेफरेंस की तुलना करता है—सही तुलना के लिए toString().equals() या Java 11+ में compareTo() == 0 का प्रयोग करें।

Syntax, Formula & Technical Example (StringBuilder Class: Mutable, Non-Synchronized and High Performance)

public class BuilderDemo {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(); // capacity = 16
        sb.append("Java").append(" ").insert(5, "21");
        System.out.println("Text: " + sb + ", Cap: " + sb.capacity());
        sb.append(" High Performance Buffer"); // grows to (16*2)+2 = 34+
        sb.delete(4, 7);
        System.out.println("Final: " + sb.toString() + ", NewCap: " + sb.capacity());
    }
}

StringBuilder Class: Mutable, Non-Synchronized and High Performance Quick Reference & Comparison Table

Constructor / MethodTechnical Behavior & EffectExam Key Note
new StringBuilder()Allocates empty buffer with default capacity 16Default no-arg constructor in java.lang
new StringBuilder("Java")Allocates initial capacity of str.length() + 16 = 20Copies string chars into mutable buffer
append(data)Adds text in place; expands to (old * 2) + 2 when fullReturns 'this' for method chaining
delete(start, end)Removes chars from inclusive start to exclusive endThrows StringIndexOutOfBoundsException if invalid

StringBuilder Class: Mutable, Non-Synchronized and High Performance Top 20 Bilingual MCQs with Answers

Q1. In which JDK version was the StringBuilder class introduced in Java?

(जावा में StringBuilder क्लास किस JDK संस्करण में पेश की गई थी?)

(A) JDK 1.2

(B) JDK 1.5 (Java 5)

(C) JDK 1.0

(D) JDK 1.8

Correct Answer: (B)

[EN] StringBuilder was introduced in JDK 1.5 as a faster, non-synchronized drop-in replacement for StringBuffer in single-threaded code.

[HI] StringBuilder को JDK 1.5 में सिंगल-थ्रेडेड कोड के लिए StringBuffer के तेज़, नॉन-सिंक्रोनाइज़्ड विकल्प के रूप में जोड़ा गया था।

Q2. What is the default initial capacity of an object created with new StringBuilder()?

(new StringBuilder() द्वारा बनाए गए ऑब्जेक्ट की डिफ़ॉल्ट प्रारंभिक क्षमता (capacity) कितनी होती है?)

(A) 32 characters

(B) 0 characters

(C) 8 characters

(D) 16 characters

Correct Answer: (D)

[EN] The parameterless constructor new StringBuilder() allocates an internal buffer capable of holding 16 characters initially.

[HI] बिना पैरामीटर वाला कंस्ट्रक्टर new StringBuilder() प्रारंभ में 16 कैरेक्टर रखने की क्षमता वाला बफर आवंटित करता है।

Q3. What will new StringBuilder("Computer").capacity() return?

(new StringBuilder("Computer").capacity() क्या मान लौटाएगा?)

(A) 8

(B) 32

(C) 16

(D) 24

Correct Answer: (D)

[EN] "Computer" has 8 characters, and new StringBuilder(String str) sets capacity to str.length() + 16, which equals 8 + 16 = 24.

[HI] "Computer" में 8 अक्षर हैं और String वाला कंस्ट्रक्टर str.length() + 16 क्षमता बनाता है, अतः 8 + 16 = 24 होगा।

Q4. What formula does StringBuilder use to compute new capacity when its current buffer of size oldCapacity overflows by 1 character?

(जब oldCapacity आकार का बफर 1 अक्षर से ओवरफ्लो होता है, तो StringBuilder नई क्षमता किस सूत्र से निकालता है?)

(A) (oldCapacity * 2) + 2

(B) oldCapacity * 2

(C) (oldCapacity * 3) / 2 + 1

(D) oldCapacity + 16

Correct Answer: (A)

[EN] When minimumCapacity exceeds current capacity, StringBuilder expands capacity to (oldCapacity * 2) + 2 (doubling plus two).

[HI] क्षमता कम पड़ने पर StringBuilder बफर को (oldCapacity * 2) + 2 सूत्र के अनुसार दोगुने से दो अधिक तक बढ़ाता है।

Q5. What does sb1.equals(sb2) evaluate to when sb1 = new StringBuilder("IA") and sb2 = new StringBuilder("IA")?

(जब sb1 = new StringBuilder("IA") और sb2 = new StringBuilder("IA") हों, तो sb1.equals(sb2) क्या लौटाएगा?)

(A) false

(B) 0

(C) true

(D) Compile-time error

Correct Answer: (A)

[EN] StringBuilder does not override Object.equals(), so sb1.equals(sb2) compares Heap memory references using ==, returning false.

[HI] StringBuilder, Object.equals() को ओवरराइड नहीं करता; इसलिए sb1.equals(sb2) दो अलग ऑब्जेक्ट के रेफरेंस जांचता है और false देता है।

Q6. Which internal backing array optimization did JDK 9 (JEP 254) introduce for StringBuilder?

(JDK 9 (JEP 254) ने StringBuilder के आंतरिक स्टोरेज में कौन सा सुधार पेश किया?)

(A) Allocated buffer in native C stack instead of Heap

(B) Stored characters directly in the String Constant Pool

(C) Replaced char[] with int[] for 32-bit Unicode

(D) Replaced char[] with byte[] plus a coder byte (LATIN1 or UTF16)

Correct Answer: (D)

[EN] Compact Strings in JDK 9 switched StringBuilder's backing storage from char[] to byte[] with a coder flag, halving memory for Latin-1 strings.

[HI] JDK 9 के Compact Strings ने char[] की जगह byte[] और coder फ्लैग का उपयोग शुरू किया, जिससे Latin-1 टेक्स्ट की मेमोरी आधी हो गई।

Q7. What is the return type of StringBuilder's append(), insert(), and reverse() methods?

(StringBuilder के append(), insert() और reverse() मेथड्स का रिटर्न टाइप क्या होता है?)

(A) boolean

(B) void

(C) String

(D) StringBuilder

Correct Answer: (D)

[EN] All primary mutator methods in StringBuilder return 'this' (type StringBuilder), allowing multiple calls to be chained cleanly.

[HI] StringBuilder के प्रमुख mutator मेथड्स 'this' (StringBuilder टाइप) लौटाते हैं, जिससे मेथड चेनिंग संभव होती है।

Q8. What is the return type of sb.substring(1, 4) on a StringBuilder object?

(StringBuilder ऑब्जेक्ट पर sb.substring(1, 4) का रिटर्न टाइप क्या होता है?)

(A) String

(B) StringBuilder

(C) CharSequence

(D) char[]

Correct Answer: (A)

[EN] substring() extracts characters from the buffer and returns an immutable String object without modifying the StringBuilder.

[HI] substring() बफर से अक्षर निकालकर एक नया अपरिवर्तनीय String ऑब्जेक्ट लौटाता है और StringBuilder को नहीं बदलता।

Q9. What will be the output of: StringBuilder sb = new StringBuilder("ABCDE"); sb.delete(1, 3); System.out.println(sb);

(StringBuilder sb = new StringBuilder("ABCDE"); sb.delete(1, 3); System.out.println(sb); का आउटपुट क्या होगा?)

(A) AE

(B) ABDE

(C) CDE

(D) ADE

Correct Answer: (D)

[EN] In delete(1, 3), start index 1 ('B') is inclusive and end index 3 ('D') is exclusive, removing 'B' and 'C' to leave "ADE".

[HI] delete(1, 3) में इंडेक्स 1 ('B') शामिल और इंडेक्स 3 ('D') बाहर रहता है; अतः 'B' व 'C' हटते हैं और "ADE" बचता है।

Q10. Which exception is thrown if someone calls new StringBuilder(-5)?

(यदि कोई new StringBuilder(-5) कॉल करता है तो कौन सा एक्सेप्शन उत्पन्न होगा?)

(A) StringIndexOutOfBoundsException

(B) NegativeArraySizeException

(C) IllegalArgumentException

(D) NullPointerException

Correct Answer: (B)

[EN] Passing a negative integer capacity directly attempts to allocate a negative-sized backing array, throwing NegativeArraySizeException.

[HI] ऋणात्मक क्षमता देने पर आंतरिक ऐरे आवंटन विफल होता है और NegativeArraySizeException उत्पन्न होता है।

Q11. Which package contains the StringBuilder class, and can another class extend StringBuilder?

(StringBuilder किस पैकेज में स्थित है और क्या कोई दूसरी क्लास इसे extend कर सकती है?)

(A) java.util; Yes, it is non-final

(B) java.io; No, it has private constructors

(C) java.lang; Yes, it is public non-final

(D) java.lang; No, because StringBuilder is final

Correct Answer: (D)

[EN] StringBuilder resides in java.lang and is declared public final, so no subclass can extend it.

[HI] StringBuilder, java.lang पैकेज में है और इसे public final घोषित किया गया है, इसलिए इसे इनहेरिट नहीं किया जा सकता।

Q12. What does sb.trimToSize() do on a StringBuilder object?

(StringBuilder ऑब्जेक्ट पर sb.trimToSize() क्या कार्य करता है?)

(A) Reduces unused buffer capacity to match the current character length

(B) Truncates the string length to 16 characters

(C) Clears all characters and resets capacity to 0

(D) Removes leading and trailing whitespace characters

Correct Answer: (A)

[EN] trimToSize() attempts to shrink the internal array capacity down to the current length() to conserve memory; it does not strip spaces.

[HI] trimToSize() खाली स्पेस नहीं हटाता, बल्कि मेमोरी बचाने के लिए अतिरिक्त बफर क्षमता को वर्तमान length() के बराबर घटाता है।

Q13. What happens when sb.setLength(6) is invoked on a StringBuilder currently holding "Hi" (length 2)?

(जब "Hi" (लंबाई 2) वाले StringBuilder पर sb.setLength(6) चलाया जाता है तो क्या होता है?)

(A) Length stays 2 and capacity becomes 6

(B) It appends four null characters ('\u0000') to reach length 6

(C) It pads the string with four space characters (' ')

(D) It throws StringIndexOutOfBoundsException

Correct Answer: (B)

[EN] Increasing length via setLength() pads the new positions with '\u0000' (null Unicode characters), whereas decreasing length truncates.

[HI] setLength() से लंबाई बढ़ाने पर खाली स्थानों में '\u0000' (नल कैरेक्टर) भर दिए जाते हैं, जबकि कम करने पर टेक्स्ट कट जाता है।

Q14. Which method was added to StringBuilder in Java 11 to enable lexicographical comparison without converting to String?

(String में बदले बिना शब्दकोश के अनुसार (lexicographical) तुलना करने के लिए Java 11 में StringBuilder में कौन सा मेथड जोड़ा गया?)

(A) match(StringBuilder another)

(B) contentEquals(StringBuilder another)

(C) compareTo(StringBuilder another)

(D) equalsIgnoreCase(StringBuilder another)

Correct Answer: (C)

[EN] Java 11 made StringBuilder implement Comparable<StringBuilder>, adding compareTo() which returns 0 for identical character sequences.

[HI] Java 11 में StringBuilder ने Comparable<StringBuilder> लागू किया, जिससे compareTo() मेथड जोड़ा गया जो समान टेक्स्ट पर 0 लौटाता है।

Q15. Why is StringBuilder faster than StringBuffer in a single-threaded loop performing 100,000 concatenations?

(सिंगल-थ्रेडेड लूप में 100,000 संयोजन करने पर StringBuilder, StringBuffer से तेज़ क्यों होता है?)

(A) StringBuilder avoids synchronized monitor lock acquisition on every method call

(B) StringBuilder pre-allocates 1 MB by default

(C) StringBuilder stores data in the CPU cache instead of Heap

(D) StringBuffer is immutable and creates new objects each iteration

Correct Answer: (A)

[EN] Both are mutable, but StringBuilder's methods lack synchronized locks, eliminating lock acquisition/release overhead per call.

[HI] दोनों परिवर्तनीय हैं, लेकिन StringBuilder मेथड्स में synchronized लॉक नहीं होता, जिससे प्रत्येक कॉल पर लॉकिंग का समय बचता है।

Q16. Which of the following statements about StringBuilder is NOT true?

(StringBuilder के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)

(A) StringBuilder implements CharSequence and Serializable

(B) StringBuilder objects are mutable and reside on the Heap

(C) StringBuilder is thread-safe because all its mutator methods are synchronized

(D) StringBuilder does not override Object.equals()

Correct Answer: (C)

[EN] StringBuilder is explicitly NOT synchronized and NOT thread-safe; thread safety via synchronization belongs to StringBuffer.

[HI] StringBuilder सिंक्रोनाइज़्ड और थ्रेड-सेफ नहीं होता; सिंक्रोनाइज़ेशन के माध्यम से थ्रेड-सेफ्टी StringBuffer में होती है।

Q17. Within StringBuilder Class: Mutable, Non-Synchronized and High Performance, which constructor / method directly maps to: "Allocates empty buffer with default capacity 16"?

(StringBuilder Class: Mutable, Non-Synchronized and High Performance में कौन सा Constructor / Method सीधे "Allocates empty buffer with default capacity 16" से संबंधित है?)

(A) append(data)

(B) new StringBuilder()

(C) new StringBuilder("Java")

(D) delete(start, end)

Correct Answer: (B)

[EN] In StringBuilder Class: Mutable, Non-Synchronized and High Performance, 'new StringBuilder()' handles 'Allocates empty buffer with default capacity 16' (Default no-arg constructor in java.lang).

[HI] StringBuilder Class: Mutable, Non-Synchronized and High Performance में 'new StringBuilder()' का कार्य/विवरण 'Allocates empty buffer with default capacity 16' (Default no-arg constructor in java.lang) है।

Q18. Which option accurately describes 'new StringBuilder("Java")' in StringBuilder Class: Mutable, Non-Synchronized and High Performance?

(StringBuilder Class: Mutable, Non-Synchronized and High Performance के संदर्भ में 'new StringBuilder("Java")' की सही विशेषता कौन सी है?)

(A) Adds text in place; expands to (old * 2) + 2 when full

(B) Allocates initial capacity of str.length() + 16 = 20

(C) Removes chars from inclusive start to exclusive end

(D) Allocates empty buffer with default capacity 16

Correct Answer: (B)

[EN] 'new StringBuilder("Java")' provides 'Allocates initial capacity of str.length() + 16 = 20' — key point: Copies string chars into mutable buffer.

[HI] 'new StringBuilder("Java")' का मुख्य विवरण 'Allocates initial capacity of str.length() + 16 = 20' है (Copies string chars into mutable buffer)।

Q19. In StringBuilder Class: Mutable, Non-Synchronized and High Performance, identify the correct parameter for the specification: "Adds text in place; expands to (old * 2) + 2 when full".

(StringBuilder Class: Mutable, Non-Synchronized and High Performance में "Adds text in place; expands to (old * 2) + 2 when full" विवरण किस विकल्प के लिए सही है?)

(A) append(data)

(B) delete(start, end)

(C) new StringBuilder()

(D) new StringBuilder("Java")

Correct Answer: (A)

[EN] 'append(data)' corresponds to 'Adds text in place; expands to (old * 2) + 2 when full' (Returns 'this' for method chaining).

[HI] 'append(data)' का संबंध 'Adds text in place; expands to (old * 2) + 2 when full' (Returns 'this' for method chaining) से है।

Q20. Considering StringBuilder Class: Mutable, Non-Synchronized and High Performance, what key behavior defines 'delete(start, end)'?

(StringBuilder Class: Mutable, Non-Synchronized and High Performance में 'delete(start, end)' की मुख्य भूमिका या मान क्या है?)

(A) Removes chars from inclusive start to exclusive end

(B) Adds text in place; expands to (old * 2) + 2 when full

(C) Allocates initial capacity of str.length() + 16 = 20

(D) Allocates empty buffer with default capacity 16

Correct Answer: (A)

[EN] 'delete(start, end)' represents 'Removes chars from inclusive start to exclusive end' (Throws StringIndexOutOfBoundsException if invalid).

[HI] 'delete(start, end)' का अर्थ/मान 'Removes chars from inclusive start to exclusive end' है (Throws StringIndexOutOfBoundsException if invalid)।

StringBuilder Class: Mutable, Non-Synchronized and High Performance — Exam FAQs

Q1: Why does sb1.equals(sb2) return false even when both StringBuilders contain the same text?

दोनों StringBuilder में समान टेक्स्ट होने पर भी sb1.equals(sb2) false क्यों लौटाता है?

[EN] StringBuilder inherits Object.equals() without overriding it, so equals() checks reference identity (sb1 == sb2). Use sb1.toString().equals(sb2.toString()) or sb1.compareTo(sb2) == 0 (Java 11+) to compare text content.

[HI] StringBuilder ने Object.equals() को ओवरराइड नहीं किया है, इसलिए यह टेक्स्ट की बजाय मेमोरी पता जांचता है। सही तुलना के लिए sb1.toString().equals(sb2.toString()) या Java 11+ में compareTo() == 0 उपयोग करें।

Q2: How should we initialize StringBuilder when the approximate final string length is known in advance?

यदि अंतिम स्ट्रिंग की अनुमानित लंबाई पहले से ज्ञात हो तो StringBuilder को कैसे बनाना चाहिए?

[EN] Pass the estimated size to new StringBuilder(estimatedSize). This prevents repeated buffer reallocations and Arrays.copyOf() operations as the string grows.

[HI] अनुमानित आकार को सीधे new StringBuilder(estimatedSize) में पास करें। इससे बार-बार बफर बढ़ाने और पुराना डेटा कॉपी करने का खर्च बच जाता है।

Q3: When is it unsafe to use StringBuilder?

StringBuilder का उपयोग कब असुरक्षित होता है?

[EN] Avoid sharing a single StringBuilder instance across multiple threads without external synchronization, as concurrent append/delete calls cause race conditions, garbled output, or index exceptions; use StringBuffer or thread-local StringBuilders instead.

[HI] बिना बाहरी लॉक के एक ही StringBuilder को कई थ्रेड्स के बीच साझा करना असुरक्षित है क्योंकि रेस कंडीशन से डेटा बिगड़ सकता है; ऐसी स्थिति में StringBuffer या प्रति-थ्रेड अलग StringBuilder उपयोग करें।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)