CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2005
    Posts
    25

    Post what is string pooling

    hi,
    Any one can tellme about what is string pooling and what are the diffeerence between "String" and "StringBuffer".


    also what is difference,when we declare.

    String s1="hello";
    String s1=new String("hello")

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: what is string pooling

    Hi

    Unfortunately I can't help you out with String Pooling, but I can answer your other questions.

    The difference between
    String s1="hello";
    String s1=new String("hello")
    is:

    If there is already a String literal equal to "hello" present in memory it will be referenced by s1 in the first statement String s1 = "hello"; For the second statement, EVEN if there is a String literal in memory equal to "hello" a new object will be created and used in String s1=new String("hello").

    The differnece between a String and a StringBuffer:
    String are immutable, ie: they cannot be changed once they have been created, StringBuffers are mutable ie: they can be changed once they have been created by using certain method calls.

    This happens because when you create a String
    Code:
    String s = new String("string");
    The object is stored in memory and referenced by s, now if you say
    Code:
    s = new String("string2");
    The original object will NOT be overwritten, instead a new instance of string will be created in memory and referenced by s but the old reference/object will still remain.

    If you need to concatenate a large number of strings it is much quicker and less memory intensive to use StringBuffers e.g:
    Code:
    StringBuffer sb = new StringBuffer("");
    sb.append("a").append("b").append("c").append("d");
    String s = sb.toString();
    This equivalent to doing this in Strings
    Code:
    String s = new String("");
    s += "a";
    s += "b";
    s += "c";
    s += "d";
    but will be much quicker when using StringBuffers. The reason for this is that because Strings are immutable each time you append to your String s += "a"; a new String object needs to be created for s in memory, where the StringBuffer will remain the same object in memory and just append literals to it.

    Hope this helps
    Byron

  3. #3
    Join Date
    Feb 2003
    Location
    Sunny Glasgow!
    Posts
    258

    Re: what is string pooling

    http://www.freshsources.com/Strings.htm
    Figure 1 is the closest I could find.
    Never heard of String Pooling though.

  4. #4
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: what is string pooling

    Here's a page with some info about the String literal pool: http://www.javaranch.com/journal/200...Literally.html
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  5. #5
    Join Date
    Dec 2007
    Posts
    1

    Smile Re: what is string pooling

    String s1="hello";
    String s1=new String("hello")

    for the first statement, it will create an object in heap
    and for the second statement, it will create an object in heap as well as create one more object in string pool.

    For the 1st statement, if we assign String s1="Hai"; then that "Hai" will overridden the "hello" but for the 2nd statement a new object will be created with "Hai" and "hello" also retain in string pool.

    If any other object say, String s2 = new String("hello") means that wont create a new object instead it will reuse the existing "hello" object.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: what is string pooling

    Quote Originally Posted by jsubaa
    If any other object say, String s2 = new String("hello") means that wont create a new object instead it will reuse the existing "hello" object.
    No, the 'new' keyword always creates a new object.

    You can force a new String to reuse a pooled instance:
    Code:
    String s2 = new String("hello").intern();
    // or
    String s2 = new String("hello");
    ...
    s2.intern();
    Having said that, I don't know why you'd want to create a new String in the first place, and if you did, why then replace it with a pooled instance?

    The outcome of any serious research can only be to make two questions grow where only one grew before...
    T. Veblen
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: what is string pooling

    Having said that, I don't know why you'd want to create a new String in the first place, and if you did, why then replace it with a pooled instance?
    I suppose it would allow you to compare 2 strings using '==' although I can't say I'd recommend doing this other than in the most extreme of circumstances.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: what is string pooling

    But why deliberately create a new String in the first place?

    Good judgment comes from experience; experience comes from bad judgment...
    F. Brooks
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: what is string pooling

    But why deliberately create a new String in the first place?
    Maybe so that you've got 2 strings that you can't compare using '=='

    [EDIT]I've just noticed this thread was started in 2005. jsubaa please don't answer threads that have expired a long time ago.
    Last edited by keang; December 7th, 2007 at 12:19 PM.

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: what is string pooling

    Quote Originally Posted by keang
    jsubaa please don't answer threads that have expired a long time ago.
    And if you must, try to get the answer right...

    If you don't think carefully, you might believe that programming is just typing statements in a programming language...
    W. Cunningham
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured