Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Fubarable

Thread: String vs stingBuffer

  1. #1
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default String vs stingBuffer

    what is the difference b/w string and string buffer with example programs?

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: String vs stingBuffer

    It is like a String but the contents of the StringBuffer can be modified after it is created. Tons of examples online.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: String vs stingBuffer


  4. #4
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: String vs stingBuffer

    Quote Originally Posted by sehudson View Post
    It is like a String but the contents of the StringBuffer can be modified after it is created. Tons of examples online.
    But If we try this following progrm:
    Java Code:
    class str
    {
        public static void main(String args[])
        {
            String s1="java";
            String s2="prg";
            s1=s2;
            System.out.println(s1);
        }
    }
    the o/p of the above prgram is Prg.
    Actually the thing is the strings are not changed and but in above prgram string s1 changed.
    So, strings are not mutable right....
    if not give an example...
    thank you
    Last edited by sunde887; 09-19-2011 at 02:39 AM. Reason: Added code tags, [code]...[/code]

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: String vs stingBuffer

    You're confusing the concept of object and reference variable. The reference variable above, s1, can change which object (the String itself) it is referring to, but the String itself is immutable (reflection cheats aside).

    For instance if you have this code:

    Java Code:
    String s1 = "java";
    String s2 = "prg";
    s1 = s2;
    Java creates two Strings, "java" and "prg" and has s1 refer to "java" and s2 refer to "prg". The last line of the code above simply has s2 refer to the "prg" String. It does nothing to "java" which still exists in the String pool. The reason this is important is if you do something like:

    Java Code:
    String myString = "0";
    for (int i = 0; i < veryBigNumber; i++) {
      myString += "0";
    } 
    System.out.println(myString);
    Java has to create a new String object each time this loop is iterated, and if this is in a time critical portion of your code, this could slow you noticeably. If on the other hand you do:

    Java Code:
    StringBuilder myStringBuilder = new StringBuilder("0");
    for (int i = 0; i < veryBigNumber; i++) {
      myStringBuilder.append("0");
    } 
    System.out.println(myStringBuilder.toString());
    Then only one StringBuilder object is created, and it is modified within the loop.
    Last edited by Fubarable; 09-18-2011 at 09:13 PM.
    sunde887 likes this.

  6. #6
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: String vs stingBuffer

    sir you said java creates a new memory for above given problem right. so, if i want to print java after code s1=s2? and
    If new memory create for above program, where the string java stored?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: String vs stingBuffer

    As far as I know the string "java" would be lost, and thus wasting memory. Since you no longer have a reference to it, you can't use it.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: String vs stingBuffer

    Quote Originally Posted by sunde887 View Post
    As far as I know the string "java" would be lost, and thus wasting memory. Since you no longer have a reference to it, you can't use it.
    Even if you lose a reference to it, most Strings are held in the String pool in case it needs to be used again. So likely it will still be in memory, at least for a while, and probably longer than most objects for whom all references have been removed.

  9. #9
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: String vs stingBuffer

    The capacity of StringBuffer is 16. So, what is the capacity of string?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: String vs stingBuffer

    I believe you read something wrong, StringBuffer/Builder can have more than 16 characters.

  11. #11
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: String vs stingBuffer

    But the intial capacity of the stringBuffer is 16 chars,isnt it?
    i read it in object-oriented programming through java by radhakrishnan.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: String vs stingBuffer

    But the intial capacity of the stringBuffer is 16 chars,isnt it?
    How does that matter?

    So, what is the capacity of string?
    String doesn't have a capacity, nor the concept of one.

    db

  13. #13
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: String vs stingBuffer

    so what is the size of string object

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 11
    Last Post: 08-15-2011, 11:12 AM
  2. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  4. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  5. How to bump the value up by one in my StingBuffer
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •