StringBuffer class and its constructors; StringBuffer methods (append(), insert(), update(), delete(), reverse(), capacity())

StringBuffer એ mutable workbench છે: append, insert, delete અને reverse ONE object ને place માં edit કરે છે, capacity 16 પર શરૂ થાય છે plus initial string ની length, અને દરેક edit પર કોઈ new object નથી.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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 16
  • new StringBuffer(50): empty, capacity 50
  • new 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)

AspectStringStringBuffer
MutabilityImmutable: દરેક edit new object બનાવે છેMutable: edits in place happen થાય છે
Best forFixed text, comparisons, keysRepeated building અને editing
Editing costદરેક change પર new objectએક object, જરૂર પડે તેમ grows
Thread safetyUnchangeable હોવાથી safeSafe: તેની methods synchronized છે

Quiz

new StringBuffer("Book").capacity() શું return કરે છે?

  1. 16
  2. 4
  3. 20
  4. 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 છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Basic Concepts of Strings and Exceptions

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

StringBuffer class and its constructors; StringBuffer methods (append(), insert(), update(), delete(), reverse(), capacity()) · Java Programming Language · Gri-Learn