Java में StringBuilder और StringBuffer
In this page:
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Inserting and Deleting
चूंकि StringBuilder mutable है, आप insert(index, value) से किसी भी position पर characters insert कर सकते हैं या delete(start, end) से एक range हटा सकते हैं बिना हर बार एक नया object बनाए, एक immutable String पर equivalent operations के विपरीत।
उदाहरण: Inserting and Deleting
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक loop में
StringBuilderके बजायString +=उपयोग करना, जो कई temporary objects बनाता है। StringBuilderobjects कोequals()से compare करना, जो references compare करता है text नहीं;toString()compare करें।- जब एक
Stringचाहिए हो तोtoString()call करना भूल जाना।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: