permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Immutable ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings Immutable ??" Watch "Strings Immutable ??" New topic
Author

Strings Immutable ??

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Strings are immutable? What does it mean? I have written this example below and created Strings and am assigning new values which they are excepting and priting.
I thought that immutable means their value cannt be changed once it is assigned. Please clarify.
public class StringImm{
public static void main(String argv[]){

String str1="one";
System.out.println(str1);
str1 = "two";
System.out.println(str1);
str1=str1+"five";
System.out.println(str1);


String str2=new String("three");
System.out.println(str2);
str2="four";
System.out.println(str2);
str2=str2+"six";
System.out.println(str2);
}
}
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Deepali Pate:
I thought that immutable means their value cannt be changed once it is assigned.

That's correct. The value of a String can not be changed once it is set. Therefore, they are considered immutable objects.
The problem is that they "seem" mutable. You can concatenate or cut them up and do whatever to them. The trick is to realize that, when you concatenate two Strings together, neither of the Strings are changed. Rather, a brand new String is created and returned to you. Check out this example:

You see, if Strings were really mutable, both s1 and s2 would contain the same value. However, when you concatenate the "World!" to s1, a brand new String is returned.
You can also do a search through this forum and find a number of posts on this topic.
I hope that helps,
Corey


SCJP Tipline, etc.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
I promise i will do the homework of using search for this topic but please tell me if 's1' is immutable then its value should not change from Hello to HelloWorld isnt it?? It should give a error ask for a new String variable.
What do u say?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Deepali Pate:
I promise i will do the homework of using search for this topic but please tell me if 's1' is immutable then its value should not change from Hello to HelloWorld isnt it?? It should give a error ask for a new String variable.
What do u say?

All Strings are immutable. Let me draw a picture to help you see what is happening.

After these first two lines are executed, we have to String variables that reference the same object. Now, when we execute the next line of code, the concatenation, a new String is created...

So, you can see that no String objects were changed. A new String object was created and the variable s1 (which is a variable, not an object) was modified to reference that new object.
Corey
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Thanks.
I am going thro the search to get better understadning of String literals, references, objects, String pool and GC on strings. All this is very confusing need patience and Conc. to get the concepts clear.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Deepali,
String is immutable mean
" a String object is immutable".
But you can have a String reference point to
a different String Object. Since you use a
String reference to represent a String object,
and since you can have s Sting reference point to
a different String object, you don't feel that a String is immutable.
So is String immutable?
The meaning of "String is immutable" is actually vague. It should be changed to
" a String object is immutable."
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Deepali,
String is immutable mean
" a String object is immutable".
But you can have a String reference point to
a different String Object. Since you use a
String reference to represent a String object,
and since you can have s Sting reference point to
a different String object, you don't feel that a String is immutable.
So is String immutable?
The meaning of "String is immutable" is actually vague. It should be changed to
" a String object is immutable."
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
when you perform any string operations, like concatonation, new string objects are created and assigned to the original object. java does this transparently, which can be confusing. run this code - it might help you understand what's going on.

also, try creating the class below, and play around with the methods. try creating obj a and obj b in the same way as above, and changing the int x field, then printing the values for each object. then try calling the methods on the String and StringBuffer objects above.

hope that helped, and didn't add to the confusion!


use the [CODE] tags - it makes it much easier for people to help you.
Larry Lecomte
Ranch Hand

Joined: Jun 14, 2002
Posts: 37
I know that the String class is immutable, but I'd like to know other classes that also are ??
For example, Integer and Boolean are immutable because I know that once they are assigned the object can't be modified, but can I detect this only with the class' modifiers and its methods ?
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
I think the only classes that are immutable are: String and the Wrapper classes.Please, anyone correct me if I�m wrong.
Francisco


Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Francisco A Guimaraes:
I think the only classes that are immutable are: String and the Wrapper classes.Please, anyone correct me if I�m wrong.
Francisco

There are more, such as the File class. I don't know if there is a definitive list anywhere. Perhaps someone knows of one. Your best bet would be to check the API Spec for a given class - it will say if the object is immutable.
Corey
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Please Deepali, and everybody in general. Note that the recycling of String literals is not going to be tested in the exam. Forget about them, trust me.
However it's very important to know that string literals and strings computed from compile constant expressions are automatically interned. And that two interned string objects can be compared with ==.


SCJP2. Please Indent your code using UBB Code
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Hi Jose,
I do realise that this is not the exam objective but when i know there is something and i have not understood it i cant study any further. So sorry for bothering u on this topic.
Can u please give me example to explain what u mean by "However it's very important to know that string literals and strings computed from compile constant expressions are automatically interned."
Thanks a lot for your time and patience.
Deepali
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi Deepali, this code may be helpful

String API There, go to the intern method
compile-time constant expression
Post any remaining doubt after having read the links.
Anyway a search on the Ranch could also be very valuable
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Jose,
Thanks a lot for ur time and patience. Looks like i am getting it clear now.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings Immutable ??
 
Similar Threads
Question about string immutable
Referencing to another object
String comparison
null Strings concatenation
String class