Theory
catalog જે 6000 copies બનાવ્યા
Month-end: BookBridge તેનો full catalog report assemble કરે છે.
String report = "";
for each book: report = report + line;
Immutability ને યાદ કરો: દરેક + brand-new String બનાવે છે અને જૂનો one ને abandon કરે છે. 6000 lines એટલે roughly 6000 discarded objects, દરેક તેના પહેલાના everything ને copy કરતા. Garbage collector clean up કરે છે, પણ તમે એને flooded room ને mop કરાવ્યું.
Theory
Printed page vs whiteboard
String એ printed page છે: એક word ને change કરવા તમારે આખું new page print કરવું પડે.
StringBuffer એ whiteboard છે: walk up કરો અને add કરો, squeeze in કરો, erase અથવા rewrite કરો, અને તે SAME whiteboard રહે છે. board એ તેના પર લખાયેલું છે તેના કરતાં થોડું મોટું પણ છે, spare room જેથી small additions ને new board ની જરૂર ન પડે. એ spare room એ તેની capacity છે.
Theory
Constructors અને capacity rule
3 constructors, memorise કરવા માટે એક rule:
new StringBuffer(): empty, capacity 16new StringBuffer(50): empty, capacity 50new StringBuffer("Let Us C"): capacity = 16 + length, અહીં 16 + 8 = 24
2 words ને apart રાખો: length() actually stored characters ને count કરે છે; capacity() allocated room ને count કરે છે. "Let Us C" ને hold કરતું buffer length 8 ધરાવે છે, capacity 24. capacity ને exceed કરો અને buffer પોતે automatically grow થાય છે; તમે ક્યારેય overflow નથી કરતા.
Practical
Editing in place
public class CatalogLine {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Let Us C");
System.out.println(sb.length()); // 8
System.out.println(sb.capacity()); // 24 (16 + 8)
sb.append(" 2nd Ed"); // same object, હવે longer
sb.insert(0, "* "); // front પર squeeze in
System.out.println(sb); // * Let Us C 2nd Ed
sb.delete(0, 2); // indexes 0,1 remove કરો (end exclusive)
sb.setCharAt(0, 'G'); // the "update" operation
System.out.println(sb); // Get Us C 2nd Ed
}
}
At a glance
String vs StringBuffer (exam table)
| Aspect | String | StringBuffer |
|---|---|---|
| Mutability | Immutable: દરેક edit new object બનાવે છે | Mutable: edits in place happen થાય છે |
| Best for | Fixed text, comparisons, keys | Repeated building અને editing |
| Editing cost | દરેક change પર new object | એક object, જરૂર પડે તેમ grows |
| Thread safety | Unchangeable હોવાથી safe | Safe: તેની methods synchronized છે |
Quiz
new StringBuffer("Book").capacity() શું return કરે છે?
- 16
- 4
- 20
- 0
Show the answer
20
String constructor 16 spare slots PLUS initial content ની length allocate કરે છે: 16 + 4 = 20. Option A એ EMPTY buffer ની capacity છે new StringBuffer() થી. Option B capacity ને length() સાથે confuse કરે છે, જે indeed 4 હોત. Option D એટલે કોઈ room નથી, જે કોઈ constructor produce નથી કરતું. 16-plus-length formula લગભગ દરેક વર્ષે some form માં asked થાય છે.
Think first
reverse ને trace કરો
StringBuffer sb = new StringBuffer("Us C"); sb.reverse(); System.out.println(sb); spaces ને include કરીને, character by character તમારા mind માં flip કરો, પછી tap કરો.
Show the answer
C sU. 4 characters U, s, space, C એ C, space, s, U બને છે. notice કરવા લાયક બે બાબતો: space એ કોઈ પણ other character જેવો છે અને move પણ થાય છે; અને reverse() એ sb ને ITSELF change કર્યું, કોઈ assignment needed નહીં, કારણ કે StringBuffer in place edit કરે છે. String પર same call exist જ નથી કરતું: String ને reverse કરવા, standard trick એ new StringBuffer(s).reverse().toString() છે, favourite 2-mark question.
Watch out
બે traps, એક honest note
Trap 1: String s = "abc"; s.concat("d"); કંઈ change નથી કરતું, પણ sb.append("d") sb ને change કરે છે. Students assign-it-back habit ને StringBuffer માં carry કરે છે અથવા, worse, no-assign habit ને પાછા String માં.
Trap 2: delete(0, 2) ફક્ત indexes 0 અને 1 ને remove કરે છે: end-exclusive, substring જેવું જ convention.
Honest note: તમારા syllabus update() method ને list કરે છે; updating માટે StringBuffer ના real names setCharAt(index, ch) અને replace(start, end, s) છે. exam માં એ લખો.
Theory
શા માટે methods buffer ને return કરે છે
append અને insert તમે જેને call કર્યું તે very buffer ને return કરે છે, એટલે calls plumbing જેવા chain થાય છે: sb.append(" 2nd Ed").insert(0, "* ").reverse(). catalog loop એ બને છે: ONE StringBuffer create કરો, 6000 lines append કરો, end પર once toString() call કરો. String version જેવું જ output, garbage નો fraction. અને "its methods are synchronized" ને file away કરો: જ્યારે Unit 4 threads ને introduce કરે છે, StringBuffer એ string tool છે જે already તેમના માટે built છે.
Summary
Key takeaways
- StringBuffer mutable છે: append, insert, delete, setCharAt, replace અને reverse એક object ને in place edit કરે છે.
- Constructors: empty (capacity 16), sized, અથવા String થી (capacity 16 + length).
- length() = stored characters; capacity() = allocated room; buffer જરૂર પડે ત્યારે પોતે grow થાય છે.
- delete અને replace end-exclusive છે, substring ને match કરે છે.
- syllabus નું update = real StringBuffer vocabulary માં setCharAt / replace.
- Fixed text માટે String વાપરો, heavy building માટે StringBuffer; String ને new StringBuffer(s).reverse().toString() દ્વારા reverse કરો.
- Memory hook: String એ printed page છે, StringBuffer એ whiteboard છે.