Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: StringBuffer Implemenatation

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default StringBuffer Implemenatation

    Hi,

    How stringbuffer implementation is being done in Java. I mean how it creates objects and appends values??


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: StringBuffer Implemenatation

    Basically the same way an Vector<Char> is implemented. It allocates more space than it thinks is necessary, then it can simply "add" items by filling in those spaces. If it reaches the limit of spaces it has available, it will create a new larger allocated area, then copy over all the old values and unallocate the old area.

    I don't use StringBuffer very often, though. More often I use the StringBuilder class which has exactly the same concept but it has no built-in thread safety, thus is much faster (same reason I don't use Vector very often but instead use ArrayList).

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: StringBuffer Implemenatation

    Go straight to the source (pun intended): java.lang: StringBuffer.java

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: StringBuffer Implemenatation

    Lets say StringBuffer sf = new StringBuffer("Hello");
    then i would append "Hi" to my stringbuffer object. I am looking for an algorythm how java searches for that Hello obj and appends the value Hi to it?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: StringBuffer Implemenatation

    I'm not sure what you're looking for... Java doesn't search for the hello obj (there isn't one), but rather looks for sf (if you're interested in how it does this, you'll need to delve into the memory model of the JVM). That object will have a field denoting the size of the current buffer and it's capacity. It will make a check to see if the value you're trying to append will overflow the buffer capacity. If it does, it re-allocates a bigger buffer and copies all current valid characters to the new buffer. Then it copies the values from what you're appending into buffer[size] to buffer[size+length(appending)] and updates the size field.

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: StringBuffer Implemenatation

    Thank u.. u answered my qn.

Similar Threads

  1. String Vs StringBuffer
    By kalees in forum Java SE APIs
    Replies: 5
    Last Post: November 6th, 2009, 02:27 AM