String Constant Pool (SCP) in Heap Memory Notes in Hindi & English : स्ट्रिंग कांस्टेंट पूल (SCP): मेमोरी बचत और रेफरेंस शेयरिंग और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम String Constant Pool (SCP) in Heap Memory (स्ट्रिंग कांस्टेंट पूल (SCP): मेमोरी बचत और रेफरेंस शेयरिंग) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The String Constant Pool (SCP) is a special deduplicating cache table maintained in Heap memory to reuse unique String literals and save RAM. Understanding literal creation, the new operator, compile-time constant folding, and intern() is vital for memory optimization and exam tracing.

अध्याय सार: स्ट्रिंग कांस्टेंट पूल (SCP) हीप मेमोरी में स्थित एक विशेष कैश क्षेत्र है जो समान स्ट्रिंग लिटरल्स को साझा करके मेमोरी बचाता है। लिटरल निर्माण, new ऑपरेटर, कंपाइल-टाइम फोल्डिंग और intern() को समझना मेमोरी ऑप्टिमाइज़ेशन और परीक्षा प्रश्नों के लिए अत्यंत महत्वपूर्ण है।

String Constant Pool (SCP) in Heap Memory: Detailed Notes & Architecture (स्ट्रिंग कांस्टेंट पूल (SCP): मेमोरी बचत और रेफरेंस शेयरिंग)

1. Purpose and JVM Memory Evolution of SCP (PermGen vs Heap) (SCP का उद्देश्य और JVM मेमोरी में इसका स्थान परिवर्तन (PermGen बनाम Heap))

[EN] Because string text dominates typical Java application memory, the JVM uses the String Constant Pool (an internal hash table implementing the Flyweight pattern) so identical literals share a single object. In Java 6 and earlier, the SCP resided inside PermGen (Permanent Generation), a fixed-size non-heap area prone to java.lang.OutOfMemoryError: PermGen space when interning many dynamic strings. Starting in Java 7, JVM engineers relocated the SCP into the main Heap memory area, and Java 8+ kept SCP in Heap even after replacing PermGen with Metaspace. Moving SCP into Heap enables the Garbage Collector to reclaim unreferenced pooled strings automatically.

[HI] सामान्य जावा एप्लिकेशन की मेमोरी में स्ट्रिंग्स का बड़ा हिस्सा होता है, इसलिए JVM फ्लाईवेट पैटर्न पर आधारित SCP द्वारा समान लिटरल्स को एक ही ऑब्जेक्ट में साझा करता है। जावा 6 और उससे पहले SCP स्थायी पीढ़ी (PermGen) में रहता था, जिसका सीमित आकार अनेक डायनामिक स्ट्रिंग्स पर OutOfMemoryError: PermGen space उत्पन्न करता था। जावा 7 से SCP को मुख्य हीप मेमोरी में स्थानांतरित कर दिया गया और जावा 8+ में Metaspace आने के बाद भी SCP हीप मेमोरी में ही बना रहा। हीप में रहने से अप्रयुक्त पूल्ड स्ट्रिंग्स को गार्बेज कलेक्टर द्वारा हटाया जा सकता है।

2. String Literal vs new String() Object Creation Count (स्ट्रिंग लिटरल बनाम new String() द्वारा बनने वाले ऑब्जेक्ट्स की संख्या)

[EN] Assigning a literal like String s1 = "Jaipur"; causes the JVM to search SCP first: if "Jaipur" exists, it returns the existing reference (0 new objects); otherwise it creates 1 object inside SCP. By contrast, String s2 = new String("Jaipur"); ALWAYS forces creation of 1 fresh String object in the regular non-pool Heap area, plus 1 object in SCP if "Jaipur" was not already pooled. Thus, when "Jaipur" is absent from SCP beforehand, new String("Jaipur") creates 2 objects total, though s2 points only to the non-pool Heap object. Calling String s3 = "Jaipur"; afterwards creates 0 objects and returns the existing SCP reference.

[HI] String s1 = "Jaipur"; लिखने पर JVM पहले SCP में खोजता है: यदि "Jaipur" पहले से है तो वही रेफरेंस मिलता है (0 नए ऑब्जेक्ट), अन्यथा SCP में 1 ऑब्जेक्ट बनता है। इसके विपरीत, String s2 = new String("Jaipur"); हमेशा सामान्य हीप क्षेत्र में 1 नया ऑब्जेक्ट बनाता है, और यदि लिटरल पहले से पूल में न हो तो 1 ऑब्जेक्ट SCP में भी बनाता है। अतः यदि "Jaipur" पहले से SCP में न हो, तो new String("Jaipur") कुल 2 ऑब्जेक्ट बनाता है, पर s2 केवल सामान्य हीप ऑब्जेक्ट की ओर संकेत करता है। इसके बाद String s3 = "Jaipur"; लिखने पर 0 नए ऑब्जेक्ट बनते हैं।

3. The String.intern() Method Mechanics (String.intern() मेथड की कार्यप्रणाली)

[EN] Calling s.intern() explicitly asks the JVM to return the canonical pooled reference matching s's character content. If an equal String already resides in SCP, intern() simply returns that existing SCP reference. If not found in SCP, since Java 7+, the JVM records a reference to that existing Heap String object directly inside the SCP table without duplicating the object and returns it. Note carefully: for String s = new String("ABC");, "ABC" was already placed in SCP during class loading, so s.intern() returns the SCP object rather than s itself.

[HI] s.intern() कॉल करने पर JVM SCP से s के समान कंटेंट वाला कैनोनिकल रेफरेंस लौटाता है। यदि SCP में समान स्ट्रिंग पहले से मौजूद हो, तो intern() उसी पूल रेफरेंस को लौटा देता है। यदि वह SCP में न हो, तो जावा 7+ में JVM बिना नया डुप्लीकेट बनाए सीधे उस हीप ऑब्जेक्ट का रेफरेंस SCP तालिका में दर्ज करके लौटा देता है। ध्यान दें कि String s = new String("ABC"); में "ABC" पहले से ही क्लास लोडिंग के समय SCP में आ चुका होता है, इसलिए s.intern() उस SCP रेफरेंस को लौटाएगा।

4. Compile-Time Constant Folding vs Runtime Concatenation (कंपाइल-टाइम कांस्टेंट फोल्डिंग बनाम रनटाइम संयोजन)

[EN] When concatenating compile-time constant literals or final constant Strings—such as "Raj" + "asthan"—javac performs constant folding and embeds "Rajasthan" directly as a single pooled literal. Therefore, String a = "Rajasthan"; and String b = "Raj" + "asthan"; evaluate a == b as true. However, if either operand is a non-final runtime variable (e.g., String p = "Raj"; String c = p + "asthan";), concatenation happens dynamically at runtime (via StringBuilder or invokedynamic since Java 9). Runtime concatenation yields a new Heap object outside SCP, so a == c evaluates to false unless intern() is called.

[HI] जब दो कंपाइल-टाइम लिटरल्स या final कांस्टेंट स्ट्रिंग्स (जैसे "Raj" + "asthan") को जोड़ा जाता है, तो javac कंपाइल-टाइम फोल्डिंग करके "Rajasthan" को एकल पूल लिटरल बना देता है। इसलिए String a = "Rajasthan"; और String b = "Raj" + "asthan"; में a == b का मान true आता है। परंतु यदि एक भी ऑपरेंड सामान्य नॉन-फ़ाइनल वेरिएबल हो (जैसे p + "asthan"), तो जोड़ना रनटाइम पर (StringBuilder या जावा 9 से invokedynamic द्वारा) होता है। रनटाइम संयोजन SCP के बाहर हीप में नया ऑब्जेक्ट बनाता है, अतः a == c का मान false होता है जब तक intern() न बुलाया जाए।

Syntax, Formula & Technical Example (String Constant Pool (SCP) in Heap Memory)

String a = "Computer";
String b = "Comp" + "uter";
String p = "Comp";
String c = p + "uter";
String d = new String("Computer");
System.out.println(a == b);          // true
System.out.println(a == c);          // false
System.out.println(a == d.intern()); // true

String Constant Pool (SCP) in Heap Memory Quick Reference & Comparison Table

Expression / ConceptMemory OutcomeExam Fact
String s = "Hi";Reuses or creates 1 object in SCPStored in Heap SCP since Java 7; GC eligible
new String("Hi")Forces 1 non-pool Heap object (+1 SCP if new)Creates up to 2 objects; reference points outside SCP
"Ja" + "va" FoldingResolved at compile time into pooled "Java"Shares identical SCP address with literal "Java"
s.intern()Returns canonical SCP reference for contentRegisters Heap ref in SCP table if absent (Java 7+)

String Constant Pool (SCP) in Heap Memory Top 20 Bilingual MCQs with Answers

Q1. Since Java 7 (and continuing in Java 8, 11, 17, 21), in which JVM memory region does the String Constant Pool reside?

(जावा 7 के बाद से (जावा 8, 11, 17, 21 सहित) स्ट्रिंग कांस्टेंट पूल JVM के किस मेमोरी क्षेत्र में स्थित है?)

(A) Main Heap memory

(B) Native Metaspace exclusively

(C) JVM Method Stack

(D) PermGen space

Correct Answer: (A)

[EN] Starting in Java 7, SCP was moved from PermGen into main Heap memory so unused pooled literals can be garbage-collected.

[HI] जावा 7 से SCP को PermGen से हटाकर मुख्य हीप मेमोरी में स्थानांतरित किया गया ताकि अप्रयुक्त लिटरल्स की गार्बेज कलेक्शन हो सके।

Q2. Assuming "Exam" is not yet in SCP, how many String objects are created in total by: String s = new String("Exam");?

(यदि "Exam" पहले से SCP में न हो, तो String s = new String("Exam"); से कुल कितने String ऑब्जेक्ट बनते हैं?)

(A) 2 objects (one in SCP and one in non-pool Heap)

(B) 3 objects

(C) 1 object only

(D) 0 objects

Correct Answer: (A)

[EN] The literal "Exam" creates 1 pooled instance in SCP and the new keyword allocates 1 distinct instance in regular Heap.

[HI] लिटरल "Exam" के लिए SCP में 1 ऑब्जेक्ट और new कीवर्ड के कारण सामान्य हीप में 1 अलग ऑब्जेक्ट, कुल 2 ऑब्जेक्ट बनते हैं।

Q3. What does this snippet print? String s1 = "Java"; String s2 = "Ja" + "va"; System.out.print(s1 == s2);

(यह कोड क्या प्रिंट करेगा? String s1 = "Java"; String s2 = "Ja" + "va"; System.out.print(s1 == s2);)

(A) Compile-time error

(B) true

(C) Throws NullPointerException

(D) false

Correct Answer: (B)

[EN] Because "Ja" and "va" are compile-time constants, javac folds them into "Java", making both point to the same SCP entry.

[HI] चूंकि "Ja" और "va" दोनों कंपाइल-टाइम कांस्टेंट हैं, javac उन्हें जोड़कर एक ही "Java" SCP एंट्री की ओर इंगित करता है।

Q4. What does this print? String x = "Ja"; String y = x + "va"; String z = "Java"; System.out.print(y == z);

(यह कोड क्या प्रिंट करेगा? String x = "Ja"; String y = x + "va"; String z = "Java"; System.out.print(y == z);)

(A) Runtime ClassCastException

(B) true

(C) false

(D) Compile-time error

Correct Answer: (C)

[EN] Because x is a non-final variable, x + "va" concatenates at runtime, creating a new Heap object outside SCP.

[HI] x एक नॉन-फ़ाइनल वेरिएबल है, इसलिए x + "va" रनटाइम पर जुड़कर SCP के बाहर हीप में नया ऑब्जेक्ट बनाता है।

Q5. What does calling s.intern() return if s was created via String s = new String("Hello");?

(यदि String s = new String("Hello"); बनाया गया हो, तो s.intern() क्या लौटाएगा?)

(A) The exact same non-pool Heap reference s

(B) A brand-new Heap copy outside SCP

(C) The reference to "Hello" stored inside the String Constant Pool

(D) null

Correct Answer: (C)

[EN] The literal "Hello" already exists in SCP, so s.intern() locates and returns the pooled reference rather than s.

[HI] लिटरल "Hello" पहले से SCP में मौजूद है, इसलिए s.intern() सामान्य हीप रेफरेंस s के बजाय SCP वाला रेफरेंस लौटाता है।

Q6. In Java 6 and earlier, why could calling intern() excessively on unique strings cause an OutOfMemoryError?

(जावा 6 और उससे पहले के संस्करणों में अत्यधिक यूनिक स्ट्रिंग्स पर intern() करने से OutOfMemoryError क्यों आ सकता था?)

(A) Heap memory did not exist before Java 7

(B) PermGen allowed a maximum of 256 strings

(C) SCP resided in fixed-size PermGen space with limited GC reclamation

(D) intern() bypassed RAM and wrote to CPU L1 cache

Correct Answer: (C)

[EN] PermGen was small and rarely collected in Java 6, so excessive dynamic interning triggered OutOfMemoryError: PermGen space.

[HI] जावा 6 में SCP सीमित आकार वाले PermGen में रहता था जहाँ गार्बेज कलेक्शन कम होता था, जिससे PermGen space भर जाता था।

Q7. What happens when: final String x = "Ja"; String y = x + "va"; String z = "Java"; System.out.print(y == z); runs?

(कोड final String x = "Ja"; String y = x + "va"; String z = "Java"; System.out.print(y == z); क्या प्रिंट करेगा?)

(A) Compile-time error

(B) false

(C) null

(D) true

Correct Answer: (D)

[EN] Declaring x as a compile-time final constant initialized with a literal lets javac fold x + "va" into pooled "Java".

[HI] x को लिटरल से इनिशियलाइज़्ड final कांस्टेंट बनाने पर javac कंपाइल टाइम में ही x + "va" को पूल्ड "Java" में बदल देता है।

Q8. How many NEW String objects are added when executing: String a = "Test"; String b = "Test"; String c = "Test";?

(String a = "Test"; String b = "Test"; String c = "Test"; चलाने पर कुल कितने String ऑब्जेक्ट बनते हैं (यदि "Test" नया हो)?)

(A) 2

(B) 1

(C) 3

(D) 0

Correct Answer: (B)

[EN] The first assignment creates 1 pooled "Test" object in SCP; b and c both reuse that identical reference.

[HI] पहला स्टेटमेंट SCP में 1 "Test" ऑब्जेक्ट बनाता है; b और c दोनों उसी एक रेफरेंस को साझा करते हैं।

Q9. Which design pattern does the String Constant Pool implement to avoid duplicate literal objects?

(डुप्लीकेट लिटरल ऑब्जेक्ट्स से बचने के लिए स्ट्रिंग कांस्टेंट पूल किस डिज़ाइन पैटर्न को लागू करता है?)

(A) Decorator pattern

(B) Flyweight pattern

(C) Observer pattern

(D) Singleton pattern

Correct Answer: (B)

[EN] The Flyweight pattern shares immutable intrinsic object instances across multiple references to minimize memory usage.

[HI] फ्लाईवेट पैटर्न मेमोरी बचाने के लिए अपरिवर्तनीय ऑब्जेक्ट इंस्टेंस को कई रेफरेंस के बीच साझा करता है।

Q10. If String s1 = new StringBuilder("IA").append("2026").toString(); String s2 = s1.intern(); String s3 = "IA2026"; (assuming "IA2026" was not previously a literal), what is s1 == s2 in Java 8+?

(यदि "IA2026" पहले से लिटरल के रूप में न हो, तो String s1 = new StringBuilder("IA").append("2026").toString(); String s2 = s1.intern(); में जावा 8+ पर s1 == s2 क्या होगा?)

(A) true

(B) Compile-time error

(C) false

(D) Throws IllegalStateException

Correct Answer: (A)

[EN] In Java 7+, if "IA2026" is not in SCP yet, s1.intern() puts s1's own Heap reference into SCP and returns s1 itself.

[HI] जावा 7+ में यदि "IA2026" पहले से SCP में नहीं है, तो intern() सीधे s1 का हीप रेफरेंस SCP में दर्ज करके वही लौटा देता है।

Q11. Can unreferenced Strings stored inside the String Constant Pool be garbage collected in Java 8 and later?

(क्या जावा 8 और उसके बाद के संस्करणों में SCP में रखे अप्रयुक्त स्ट्रिंग्स को गार्बेज कलेक्टर हटा सकता है?)

(A) No, SCP objects stay immortal until JVM shutdown

(B) Only when System.exit(0) runs

(C) Yes, because SCP lives in Heap memory and tracks references safely

(D) Only if created using StringBuffer

Correct Answer: (C)

[EN] Relocating SCP into Heap allowed GC to reclaim unreachable pooled Strings just like other Heap objects.

[HI] SCP के हीप में आने के बाद गार्बेज कलेक्टर अन्य हीप ऑब्जेक्ट्स की तरह अप्रयुक्त पूल्ड स्ट्रिंग्स की मेमोरी भी मुक्त कर सकता है।

Q12. What does String s1 = "A"; String s2 = s1.concat("B"); String s3 = "AB"; System.out.print(s2 == s3); print?

(String s1 = "A"; String s2 = s1.concat("B"); String s3 = "AB"; System.out.print(s2 == s3); क्या प्रिंट करेगा?)

(A) true

(B) Compile-time error

(C) AB

(D) false

Correct Answer: (D)

[EN] String.concat() executes at runtime and allocates a fresh Heap String outside SCP, distinct from pooled "AB".

[HI] String.concat() रनटाइम पर SCP के बाहर हीप में नया ऑब्जेक्ट बनाता है, इसलिए वह पूल्ड "AB" के समान रेफरेंस नहीं होता।

Q13. Why did Java 8 remove PermGen and introduce Metaspace while keeping SCP in Heap?

(जावा 8 में PermGen हटाकर Metaspace लाने के बावजूद SCP को हीप में ही क्यों रखा गया?)

(A) Because Metaspace cannot store ASCII bytes

(B) Because Heap is stored inside CPU cache

(C) Because Java 8 removed the String Constant Pool

(D) Because SCP holds String instances (objects) that benefit from unified Heap GC tuning and scaling

Correct Answer: (D)

[EN] Metaspace stores class metadata in native memory, whereas String objects remain managed on the Java Heap.

[HI] Metaspace नेटिव मेमोरी में क्लास मेटाडेटा रखता है, जबकि String ऑब्जेक्ट्स हीप गार्बेज कलेक्शन के अधीन बेहतर रहते हैं।

Q14. Which of the following is NOT true about the String Constant Pool (SCP)?

(स्ट्रिंग कांस्टेंट पूल (SCP) के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)

(A) SCP stores only unique String values without duplicate literals

(B) String literals are automatically placed in SCP

(C) intern() allows dynamically constructed Strings to leverage SCP sharing

(D) Using new String("abc") never interacts with the String Constant Pool at all

Correct Answer: (D)

[EN] The literal "abc" passed to new String("abc") still ensures "abc" exists in SCP, even though the new instance itself sits outside SCP.

[HI] new String("abc") में भी लिटरल "abc" SCP में रखा जाता है (यदि पहले से न हो), भले ही नया ऑब्जेक्ट SCP के बाहर बने।

Q15. How many Heap+SCP String objects exist after: String a = new String("X"); String b = new String("X"); (with "X" initially absent)?

(यदि "X" पहले से पूल में न हो, तो String a = new String("X"); String b = new String("X"); के बाद "X" के कुल कितने String ऑब्जेक्ट मौजूद होंगे?)

(A) 3 objects (1 in SCP + 2 separate Heap instances)

(B) 2 objects

(C) 1 object

(D) 4 objects

Correct Answer: (A)

[EN] The literal "X" creates 1 SCP entry reused by class bytecode, while each new creates 1 separate Heap instance (1 + 2 = 3).

[HI] लिटरल "X" के लिए SCP में 1 एंट्री बनती है तथा दोनों new कॉल्स से सामान्य हीप में 2 अलग ऑब्जेक्ट बनते हैं (1 + 2 = 3)।

Q16. Which JVM option or tuning factor matters if an application interns millions of Strings?

(यदि कोई एप्लिकेशन लाखों स्ट्रिंग्स को intern() करता है, तो कौन-सा JVM गुण प्रभावित होता है?)

(A) StringTableSize (hash table bucket count inside JVM)

(B) StackOverflowDepth

(C) JITInlineDisable

(D) ThreadPriorityMax

Correct Answer: (A)

[EN] Internally SCP is implemented as a hash table whose bucket count can be tuned via -XX:StringTableSize to avoid hash collisions.

[HI] आंतरिक रूप से SCP एक हैश टेबल है जिसकी बकेट संख्या को हैश टकराव कम करने हेतु -XX:StringTableSize से ट्यून किया जाता है।

Q17. Within String Constant Pool (SCP) in Heap Memory, which expression / concept directly maps to: "Reuses or creates 1 object in SCP"?

(String Constant Pool (SCP) in Heap Memory में कौन सा Expression / Concept सीधे "Reuses or creates 1 object in SCP" से संबंधित है?)

(A) "Ja" + "va" Folding

(B) s.intern()

(C) new String("Hi")

(D) String s = "Hi";

Correct Answer: (D)

[EN] In String Constant Pool (SCP) in Heap Memory, 'String s = "Hi";' handles 'Reuses or creates 1 object in SCP' (Stored in Heap SCP since Java 7; GC eligible).

[HI] String Constant Pool (SCP) in Heap Memory में 'String s = "Hi";' का कार्य/विवरण 'Reuses or creates 1 object in SCP' (Stored in Heap SCP since Java 7; GC eligible) है।

Q18. Which option accurately describes 'new String("Hi")' in String Constant Pool (SCP) in Heap Memory?

(String Constant Pool (SCP) in Heap Memory के संदर्भ में 'new String("Hi")' की सही विशेषता कौन सी है?)

(A) Forces 1 non-pool Heap object (+1 SCP if new)

(B) Resolved at compile time into pooled "Java"

(C) Reuses or creates 1 object in SCP

(D) Returns canonical SCP reference for content

Correct Answer: (A)

[EN] 'new String("Hi")' provides 'Forces 1 non-pool Heap object (+1 SCP if new)' — key point: Creates up to 2 objects; reference points outside SCP.

[HI] 'new String("Hi")' का मुख्य विवरण 'Forces 1 non-pool Heap object (+1 SCP if new)' है (Creates up to 2 objects; reference points outside SCP)।

Q19. In String Constant Pool (SCP) in Heap Memory, identify the correct parameter for the specification: "Resolved at compile time into pooled "Java"".

(String Constant Pool (SCP) in Heap Memory में "Resolved at compile time into pooled "Java"" विवरण किस विकल्प के लिए सही है?)

(A) new String("Hi")

(B) "Ja" + "va" Folding

(C) s.intern()

(D) String s = "Hi";

Correct Answer: (B)

[EN] '"Ja" + "va" Folding' corresponds to 'Resolved at compile time into pooled "Java"' (Shares identical SCP address with literal "Java").

[HI] '"Ja" + "va" Folding' का संबंध 'Resolved at compile time into pooled "Java"' (Shares identical SCP address with literal "Java") से है।

Q20. Considering String Constant Pool (SCP) in Heap Memory, what key behavior defines 's.intern()'?

(String Constant Pool (SCP) in Heap Memory में 's.intern()' की मुख्य भूमिका या मान क्या है?)

(A) Forces 1 non-pool Heap object (+1 SCP if new)

(B) Returns canonical SCP reference for content

(C) Reuses or creates 1 object in SCP

(D) Resolved at compile time into pooled "Java"

Correct Answer: (B)

[EN] 's.intern()' represents 'Returns canonical SCP reference for content' (Registers Heap ref in SCP table if absent (Java 7+)).

[HI] 's.intern()' का अर्थ/मान 'Returns canonical SCP reference for content' है (Registers Heap ref in SCP table if absent (Java 7+))।

String Constant Pool (SCP) in Heap Memory — Exam FAQs

Q1: Does SCP also apply to StringBuffer or StringBuilder objects?

क्या String Constant Pool (SCP) StringBuffer या StringBuilder ऑब्जेक्ट्स पर भी लागू होता है?

[EN] No. SCP works exclusively for immutable java.lang.String objects; StringBuffer and StringBuilder are mutable and always reside in regular Heap memory.

[HI] नहीं। SCP केवल अपरिवर्तनीय java.lang.String के लिए होता है; StringBuffer और StringBuilder परिवर्तनीय होने के कारण हमेशा सामान्य हीप मेमोरी में बनते हैं।

Q2: Should developers manually call .intern() on every String?

क्या डेवलपर्स को हर String पर मैनुअल रूप से .intern() कॉल करना चाहिए?

[EN] No. Calling intern() on unique or short-lived strings slows performance due to hash table synchronization and increases Heap memory pressure; use it only for highly repetitive long-lived identifiers.

[HI] नहीं। हर यूनिक या अल्पकालिक स्ट्रिंग पर intern() करने से हैश टेबल लॉक व मेमोरी दबाव बढ़ता है; इसका उपयोग केवल बार-बार दोहराए जाने वाले दीर्घकालिक डेटा के लिए करें।

Q3: How does Java 8's G1GC String Deduplication differ from SCP?

जावा 8 का G1GC String Deduplication फीचर SCP से किस प्रकार भिन्न है?

[EN] SCP shares entire String wrapper references, whereas G1GC deduplication (-XX:+UseStringDeduplication) shares only the underlying internal char[]/byte[] backing arrays across distinct String objects during GC.

[HI] SCP पूरे String ऑब्जेक्ट रेफरेंस को साझा करता है, जबकि G1GC स्ट्रिंग डिडुप्लिकेशन अलग-अलग String ऑब्जेक्ट्स के भीतर मौजूद समान char[]/byte[] एरे को साझा करता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)