Escolar Documentos
Profissional Documentos
Cultura Documentos
Java FAQ
Recent Topics
Register / Login
Author
Strings Immutable ??
posted 6/18/2002 6:21 PM
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);
1/7
7/15/13
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:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 .
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
SC JP 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.
view plain c opy to c lipboard print ?
www.coderanch.com/t/238260/java-programmer-SCJP/certification/Strings-Immutable
2/7
7/15/13
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 .
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...
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .
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."
3/7
7/15/13
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.
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 .
p u b l i cc l a s sf o o { p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ) { S t r i n gf o o 1=n e wS t r i n g ( " s o m et e x t " ) ; S t r i n gf o o 2=f o o 1 ; / *t h i sa c t u a l l yc r e a t e san e wo b j e c tw i t ht h es a m e v a l u ea sf o o 1a n da s s i g n sar e f e r e n c et of o o 2 . * / S y s t e m . o u t . p r i n t l n ( " f o o 1 :" + f o o 1 ) ; S y s t e m . o u t . p r i n t l n ( " f o o 2 :" + f o o 2 ) ; / *t h e s ew i l lp r i n tt h es a m et h i n g* / f o o 2=" c h a n g e d " ; / *s i n c ef o o 1a n df o o 2p o i n tt od i f f e r e n to b j e c t s ,f o o 2w i l ln o w h a v ead i f f e r e n tv a l u et h a nf o o 1 * / S y s t e m . o u t . p r i n t l n ( " f o o 1 :" + f o o 1 ) ; S y s t e m . o u t . p r i n t l n ( " f o o 2 :" + f o o 2 ) ; S t r i n g B u f f e rs b 1=n e wS t r i n g B u f f e r ( " b u f f e r " ) ; S t r i n g B u f f e rs b 2=s b 1 ; / *t h i s ,h o w e v e r ,a s s i g n sar e f e r e n c et ot h eo b j e c ti n i t i a l l y c r e a t e db ys b 1t os b 2 . T h i sm a yl o o kt h es a m ea st h eS t r i n g e x a m p l e ,b u ts e eb e l o wf o rt h ed i f f e r e n c e .* / / *t h e s ew i l lp r i n tt h es a m et h i n g* / S y s t e m . o u t . p r i n t l n ( " s b 1 :" + s b 1 ) ; S y s t e m . o u t . p r i n t l n ( " s b 2 :" + s b 2 ) ; s b 2 . a p p e n d ( " e d " ) ; / *s i n c es b 1a n ds b 2p o i n tt ot h es a m eo b j e c t ,c h a n g i n go n ei s t h e s a m ea sc h a n g i n gt h eo t h e r . t h e yw i l ln o wb o t hp r i n t" b u f f e r e d " * / S y s t e m . o u t . p r i n t l n ( " s b 1 :" + s b 1 ) ; S y s t e m . o u t . p r i n t l n ( " s b 2 :" + s b 2 ) ; } }
2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 .
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. www.coderanch.com/t/238260/java-programmer-SCJP/certification/Strings-Immutable
4/7
7/15/13
each object. then try calling the methods on the String and StringBuffer objects above.
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .
use the [C ODE] tags - it makes it much easier for people to help you.
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 Im wrong. Francisco
Francisco<br />SC JP<br />please use the [code][/code] tags when showing code.C lick <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 Im 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
Joined: Jul 03, going to be tested in the exam. Forget about them, trust me. 2001 www.coderanch.com/t/238260/java-programmer-SCJP/certification/Strings-Immutable
Please Deepali, and everybody in general. Note that the recycling of String literals is not
5/7
7/15/13
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 ==.
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
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 .
p u b l i cc l a s sT e s t { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { f i n a lS t r i n gc o n s t a n t=" o " ; S t r i n gv a r i a b l e=" o " ; S y s t e m . o u t . p r i n t l n ( " h e l l o "= =n e wS t r i n g ( " h e l l o " ) ) ; / / f a l s e S y s t e m . o u t . p r i n t l n ( " h e l l o "= =n e wS t r i n g ( " h e l l o " ) . i n t e r n ( ) ) ; / / t r u e S y s t e m . o u t . p r i n t l n ( " h e l l o "= =" h e l l "+c o n s t a n t ) ; / / t r u e S y s t e m . o u t . p r i n t l n ( " h e l l o "= =" h e l l "+v a r i a b l e ) ; / / f a l s e } }
0 7 . 0 8 . 0 9 . 1 0 .
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
www.coderanch.com/t/238260/java-programmer-SCJP/certification/Strings-Immutable
6/7
7/15/13
Similar Threads String comparison String class Referencing to another object null Strings concatenation Question about string immutable
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 15, 2013 07:55:16 .
www.coderanch.com/t/238260/java-programmer-SCJP/certification/Strings-Immutable
7/7