Results 1 to 5 of 5
  1. #1
    elektronika's Avatar
    elektronika is offline Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    17
    Rep Power
    0

    Default What is Immutable in String

    Dear All,

    I learn in the Sun java tutorial, there was something like this :
    Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.
    What does it mean by immutable? The contents of the string can easily be changed.
    Java Code:
    String newstrings;
    newstrings = " What do you mean";
    System.out.println(newstrings);
    newstrings = " With that?";
    System.out.println(newstrings);
    Java Code:
     What do you mean
     With that?
    Kindest Regards,




    Elektronika

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    No, that is not changing the contents of the String. That is changing the Object that the reference variable points to. A String is immutable in that you cannot change the contents of a String object, it has nothing to do with changing the value of a reference.

  3. #3
    elektronika's Avatar
    elektronika is offline Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    17
    Rep Power
    0

    Default

    Dear masijade,

    So what does the
    so that once it is created a String object cannot be changed
    means?

    so before it is created it can be changed? and what does the changed here means...

    Kindest Regards,




    Elektronika

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    A String is an object that "wraps" a character array. What is "immutable" about a String is that you cannot change those contents. A variable is not a String. A variable is simply a placeholder in which a pointer to a String can be saved. Changing that pointer does not change the String, it simply changes which String the variable now points to. And before it is created there is nothing to change, of course.

    See http://www.javaranch.com/campfire/StoryCups.jsp and http://www.javaranch.com/campfire/StoryPassBy.jsp

  5. #5
    elektronika's Avatar
    elektronika is offline Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    17
    Rep Power
    0

    Default

    Oh..thank you masijade!
    I also found your link useful :)

    Kindest Regards,




    Elektronika

Similar Threads

  1. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  2. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 PM
  3. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 03: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
  •