The Artima Developer Community
Sponsored Link

Java Answers Forum
Immutable String help

2 replies on 1 page. Most recent reply: Nov 12, 2003 12:34 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Immutable String help Posted: Nov 12, 2003 9:18 AM
Reply to this message Reply
Advertisement
I understand the concept of a String and immutability. However, I was under the impression that you could not assign a value as follows:

String str1 = "Hello World";

and then change the contents of it by reassigning as:

str1 = "GoodBye World";

now what I have been told is that we are actually constructing a new String object.

So I would just like some sort of explanation as to why this would be possible, because even though we may not be changing the contents of a string, we may assign what we thought was the value in the first version to another string object and actually get the value of the second assignment.

The reason this seems so baffling is that even though technically I am not changing the contents of the first string, i am assigning a new value to the string variable in effect changing what was there before. I expected to get some sort of Immutable String error.

can someone point out the error of my thinking please???


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Immutable String help Posted: Nov 12, 2003 11:23 AM
Reply to this message Reply
When you write:

String str = "hello";

then there are two things that you are dealing with. One is the variable str. Here, str is a reference variable, which can hold a reference to any String object. Note that I am saying str is a "variable". You can always change the value of str because it is a variable. By string immutability it means the string object that is constructed is immutable. So the real string object that hold the value "Hello" is immutable and not the reference variable str (There is more inside to this explanation).

Let us take a real world example, say, human being is immutable. Let us say we have a Human class and you are one of its instances. Syntactically we write it as:

Human Guido = your_physical_being;

You can understand that Guido is just your name and not the real human being object we are referring to. Here, your_physical_being is immutable and we cannot change it. However, we can start calling another human as Guido any time. It is just a name. So, when we say,

Guido = kishori_physical_being;

Here, I took your name and give that name to myself. Did it change anything to you. No. Now, by Guido it is me who is recognized and not you. So, what happens to you now. If you have another name then you may still be recorgnized. If not, then get ready to be garbage collected.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Immutable String help Posted: Nov 12, 2003 12:34 PM
Reply to this message Reply
You might have had some experience with strings in C, which are simply arrays of character and are mutable. If so, maybe this snippet will also help clarify:
class StringStuff
{
   public static void main(String args[])
   {
      String str = "Hello";
      
      // str[0] = 'J'; // Nope!  Can't do this!
 
      // So, here's how you'd kind of simulate the way strings work in C:
      char [] chars = str.toCharArray(); // Create a char array and copy the String's characters into it.
      chars[0] = 'J';  // This is okay.  The char array is mutable.
      str = new String(chars);  // str is now assiged to a new string created from the array of characters.
      System.out.println( str );
   }
}

Note that the original String "Hello" was never changed. It was copied into an array, the array was changed and a new String was created from that. When the str variable was reassigned to the new String created from the character array, the old String "Hello" still existed somewhere in memory, but was unreferenced (no variable "pointed" to it any more).

Flat View: This topic has 2 replies on 1 page
Topic: unable to fine ejb after deploying it properly Previous Topic   Next Topic Topic: Record In Jsp & back end differ for same  query

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use