Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: Strings are Immutable

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Strings are Immutable

    Strings are immutable.What does it mean ? I googled it but I can't understand what actually this sentence is saying.Please tell me in simple words and don't provide link to read and how Integers or other primitive datatypes are not immutable
    Last edited by allaudin; 07-01-2013 at 09:10 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Strings are Immutable

    It means that you can't change the contents of a String. e.g.

    Java Code:
    String s= "hello"; // s refers to "hello"
    t= s;
    s+= " world"; // now s refers to another String "hello world"
    // t still refers to "hello"
    kind regards,

    Jos
    allaudin likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Strings are Immutable

    and why strings are immutable ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Strings are Immutable

    Quote Originally Posted by allaudin View Post
    and why strings are immutable ?
    One reason: Strings are often used as keys (e.g. in a Map) and you don't want keys to change their value; another reason: Strings can be shared, therefore reducing memory consumption (read the API documentation for the String.intern() method for details).

    kind regards,

    Jos
    allaudin likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Strings are Immutable

    Quote Originally Posted by JosAH View Post
    One reason: Strings are often used as keys (e.g. in a Map) and you don't want keys to change their value.
    That. Actually I never really thought about it from the key perspective. Now I realize I have some possible failure points in applications I've built since I'm using mutable objects as the key. Generally only internal inside a method though, so it can't really hurt there.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default Re: Strings are Immutable

    Quote Originally Posted by gimbal2 View Post
    That. Actually I never really thought about it from the key perspective. Now I realize I have some possible failure points in applications I've built since I'm using mutable objects as the key. Generally only internal inside a method though, so it can't really hurt there.
    Most people do that (I do it too); if you leave those keys unchanged everthing will be fine; otherwise paranoia wins: you have to wrap your mutable objects in an immutable proxy or wrapper so you can be sure of code bloat ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Strings are Immutable

    Or just use a uniquely identifiable property of the object as the key, if available ;)

Similar Threads

  1. immutable class
    By yanivdan98 in forum New To Java
    Replies: 3
    Last Post: 02-25-2013, 12:02 AM
  2. Strings and Immutable
    By al_Marshy_1981 in forum New To Java
    Replies: 19
    Last Post: 06-18-2010, 08:22 AM
  3. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 01:58 PM
  4. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 09:12 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 04:08 PM

Posting Permissions

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