Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Immutable String

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Immutable String

    Hello, I am a beginner and can not understand this:

    String s = "Skakal pes pres oves";
    s = s.substring(0, 6);
    System.out.println(s);
    String concat = s + " pes";
    System.out.println(concat);
    System.out.println();

    It is an example code from a site. From what I know is, that the content of an immutable string can not be changed. But doesn't the s = s.substring(0, 6) changed it? Or am I misunderstanding this?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Immutable String

    The literal String "Skakal pes pres oves" is stored in memory.
    The String variable s references the literal String in memory.
    When you assign a new value to the s variable, you do not alter the previously referenced literal String, but rather you create a new literal String in memory and reassign what the s variable is referencing. The previous reference still exists in memory. You now have two literal Strings in memory, but only the second one has a reference pointing to it.

    **Note: I think the substring method actually does something a bit different than what I described, but your question was more about reassigning a String variable than the nuts and bolts of what the substring method does**
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Immutable String

    You can also used the hashcode() method to check it. each object has space in memory and can access it through memory address(I think that is
    where the variable pointing).
    print the hashcode() of your variable every before and after altering its value. you will notice that the hashcode() of the variable changes every alteration since that variable points to different memory address every alteration (because of being immutable of String)

    you can also do some experiment in StringBuilder in java, StringBuilder is mutable unlike String. print hashcode() of your variable of StringBuilder every
    before and after alteration, you'll see the difference between two.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Immutable String

    You can also used the hashcode() method to check it. each object has space in memory and can access it through memory address(I think that is
    where the variable pointing).
    print the hashcode() of your variable every before and after altering its value. you will notice that the hashcode() of the variable changes every alteration since that variable points to different memory address every alteration (because of being immutable of String)

    you can also do some experiment in StringBuilder in java, StringBuilder is mutable unlike String. print hashcode() of your variable of StringBuilder every
    before and after alteration, you'll see the difference between two.
    Not exactly. From the API of the hashcode() method for String:

    The hash code for a String object is computed as

    s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]


    using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
    In other words, the hashcode for this class doesn't directly refer to memory - doing so would be pretty catastrophic in terms of data structures that rely on the hashcode of an object (such as Maps or Sets). That being said, see the API for Object, whose hashcode implementation may be an integer representation of memory - however classes may often override this default implementation.

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Immutable String

    Thanks for the correction

  6. #6
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Immutable String

    To add to aussiemcgr's reply, take a look at String (Java Platform SE 6): "Returns a new string that is a substring of this string".

    To digress slightly, as a general tip, sometimes the API documentation may not clearly describe what a method really does, in which case it can be helpful to look into the actual code itself. Oracle's JDK comes with a file called src.zip that you can look up the code implementation for methods such as substring. On top of that, if you're using Eclipse, you can associate rt.jar (under JRE System Library) to src.zip so that you can "open declaration (F3)" a library method to jump straight into its code in src.zip.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Immutable String

    The code for the substring method actually isn't that nice. What the substring method "really" does is create a new String object which references the same literal String in memory, but adjusts some pointer indexes or something. It's not as cut-and-dry as you'd expect it to be.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Immutable String

    Quote Originally Posted by aussiemcgr View Post
    The code for the substring method actually isn't that nice. What the substring method "really" does is create a new String object which references the same literal String in memory, but adjusts some pointer indexes or something. It's not as cut-and-dry as you'd expect it to be.
    Indeed. Sorry for the self-promotion, but I wrote an article a while back that describes when to construct a new String object, and goes into detail about what goes on under the hood of this class, using the Reflection API to demonstrate this behavior.

Similar Threads

  1. mutable and immutable concept of java
    By javaDev90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 10th, 2013, 06:53 AM
  2. Immutable Integers
    By aussiemcgr in forum Java Theory & Questions
    Replies: 9
    Last Post: September 2nd, 2013, 08:21 PM
  3. immutable
    By chalapathi in forum Java Theory & Questions
    Replies: 2
    Last Post: May 7th, 2012, 09:31 AM
  4. immutable
    By saurabhRBMI in forum Java Theory & Questions
    Replies: 2
    Last Post: October 1st, 2011, 11:23 AM
  5. Replies: 3
    Last Post: July 24th, 2011, 06:13 AM