String Immutable - Explain

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheelaprabu
    New Member
    • Mar 2008
    • 1

    #1

    String Immutable - Explain

    I Knew Java String is Immutable. Because we cannt change the value of String
    object when it was created. explain with one code . thank you
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by sheelaprabu
    I Knew Java String is Immutable. Because we cannt change the value of String
    object when it was created. explain with one code . thank you
    A lot of String instantiations share their char[] data buffer without knowing who their
    co-owners of the buffer are. Changing the buffer would result in changing all the
    co-owners' buffers and result in a mess.

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by sheelaprabu
      I Knew Java String is Immutable. Because we cannt change the value of String
      object when it was created. explain with one code . thank you
      Explain what?

      * The definition of "immutable" ?
      * The design decision that String ought to be immutable?
      * How String's API demonstrates string is immutable?

      Comment

      • gaya3
        New Member
        • Aug 2007
        • 184

        #4
        Originally posted by BigDaddyLH
        Explain what?

        * The definition of "immutable" ?
        * The design decision that String ought to be immutable?
        * How String's API demonstrates string is immutable?
        Hi,
        I have some explanation to answer your query..
        i) Definition of "immutable" : "immutable" means "unchange" or "not able to change".
        ii)String ought to be immutable:
        please try out the following example..it explains good...
        [CODE=Java]class String_immutabl e{
        public sttaic void main(String args[]){
        String s1 = new String("StringV alue");
        StringBuffer s2 = new StringBuffer("S tring buffer value");
        s1.replace('a', 'b');
        s2.replace(0,2, "zzz");
        System.out.prin tln("\n String value after replace "+s1+" String buffer
        after replace "+s2);
        }
        }[/CODE]
        From above v clearly understand that "String" values are immutable and where as "String buffer" values are mutable.

        iii) Api's:
        you may try out with any API's . for better understanding "replace()" can be used..

        -Thanks & Regards,
        Hamsa

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          Originally posted by gaya3
          Hi,
          I have some explanation to answer your query..
          i) Definition of "immutable" : "immutable" means "unchange" or "not able to change".
          ii)String ought to be immutable:
          please try out the following example..it explains good...
          class String_immutabl e{
          public sttaic void main(String args[]){
          String s1 = new String("StringV alue");
          StringBuffer s2 = new StringBuffer("S tring buffer value");
          s1.replace('a', 'b');
          s2.replace(0,2, "zzz");
          System.out.prin tln("\n String value after replace "+s1+" String buffer
          after replace "+s2);
          }
          }
          From above v clearly understand that "String" values are immutable and where as "String buffer" values are mutable.

          iii) Api's:
          you may try out with any API's . for better understanding "replace()" can be used..

          -Thanks & Regards,
          Hamsa
          Please use code tags, this thread might be useful for future members

          Sukatoa ;-)

          Comment

          Working...