String Vs. StringBuffer

Discussion in 'Java' started by gaurav v bagga, Jan 18, 2007.

  1. hi,

    method(){
    String a="";
    String c="";
    }


    method(){
    StringBuffer a=new StringBuffer();
    StringBuffer c=new StringBuffer();;
    }

    will String a,c get garbage collected after method
    what about StringBuffer ?

    regards
    gaurav
     
    gaurav v bagga, Jan 18, 2007
    #1
    1. Advertising

  2. gaurav v bagga

    Abhi Guest

    gaurav v bagga wrote:
    > hi,
    >
    > method(){
    > String a="";
    > String c="";
    > }
    >
    >
    > method(){
    > StringBuffer a=new StringBuffer();
    > StringBuffer c=new StringBuffer();;
    > }
    >
    > will String a,c get garbage collected after method
    > what about StringBuffer ?
    >
    > regards
    > gaurav


    Gaurav u cannt access String a,c after method 1 as their scope is
    limited to that method only.as for garbage collectn u dont hav to
    worry.it runs periodically so u cannt say xactly when they will be
    garbaged.

    Regards........
     
    Abhi, Jan 18, 2007
    #2
    1. Advertising

  3. gaurav v bagga

    Chris Dollin Guest

    gaurav v bagga wrote:

    (call this method A)

    > method(){
    > String a="";
    > String c="";
    > }
    >


    (call this method B)

    > method(){
    > StringBuffer a=new StringBuffer();
    > StringBuffer c=new StringBuffer();;
    > }
    >
    > will String a,c get garbage collected after method
    > what about StringBuffer ?


    When (A) has finished, the variables `a` and `b` vanish.
    The variables are not garbage collected, but their values
    might be. However, "" is a string literal, hence interned,
    hence it doesn't go away until the JVM finishes.

    When (B) finishes, it's `a` and `b` vanish and their values
    become eligible for garbage collection. Since there are no
    other references to these StringBuffers, eventually (perhaps
    for very small values of "eventually", perhaps for large ones)
    they will be reclaimed.

    If, in (A), you had say written

    String a = new String( "" );

    then you would have made a /copy/ of the empty string, and once
    this (A) finished, that copy can be reclaimed. (Off-hand I can't
    remember whether the copy has a copy of the underlying char[].)

    --
    Chris "electric hedgehog" Dollin
    "The path to the web becomes deeper and wider" - October Project
     
    Chris Dollin, Jan 18, 2007
    #3
  4. gaurav v bagga

    Alex Hunsley Guest

    Abhi wrote:
    > gaurav v bagga wrote:
    >> hi,
    >>
    >> method(){
    >> String a="";
    >> String c="";
    >> }
    >>
    >>
    >> method(){
    >> StringBuffer a=new StringBuffer();
    >> StringBuffer c=new StringBuffer();;
    >> }
    >>
    >> will String a,c get garbage collected after method
    >> what about StringBuffer ?
    >>
    >> regards
    >> gaurav

    >
    > Gaurav u cannt access String a,c after method 1 as their scope is
    > limited to that method only.as for garbage collectn u dont hav to
    > worry.it runs periodically so u cannt say xactly when they will be
    > garbaged.


    You what?
     
    Alex Hunsley, Jan 18, 2007
    #4
  5. gaurav v bagga

    Adam Maass Guest

    "Chris Dollin" <> wrote:
    >
    > String a = new String( "" );
    >
    > then you would have made a /copy/ of the empty string, and once
    > this (A) finished, that copy can be reclaimed. (Off-hand I can't
    > remember whether the copy has a copy of the underlying char[].)
    >


    One of the uses of the String(String) constructor is that it constructs a
    brand-new non-shared char[] just large enough to hold the characters
    comprising the String. It's an implementation detail, but important enough
    that it is worth remembering.

    -- Adam Maass
     
    Adam Maass, Jan 18, 2007
    #5
  6. gaurav v bagga

    Lew Guest

    Abhi wrote:
    >> Gaurav u cannt access String a,c after method 1 as their scope is
    >> limited to that method only.as for garbage collectn u dont hav to
    >> worry.it runs periodically so u cannt say xactly when they will be
    >> garbaged.


    Alex Hunsley wrote:
    > You what?


    F u cn rd ths u cn b secry

    - Lew
     
    Lew, Jan 19, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    3
    Views:
    1,854
  2. John Galt

    hash for String and StringBuffer

    John Galt, Feb 24, 2004, in forum: Java
    Replies:
    4
    Views:
    552
    Doug Pardee
    Feb 25, 2004
  3. Darren
    Replies:
    5
    Views:
    4,474
    Darren
    Jul 28, 2004
  4. vj
    Replies:
    10
    Views:
    1,806
    Chris Uppal
    Mar 30, 2007
  5. gaurav_kant
    Replies:
    0
    Views:
    304
    gaurav_kant
    Sep 28, 2007
Loading...

Share This Page