• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

string immutability

>>>>>public class ImString{
public static void main(String argv[]){
        String s1 = new String("Hello");
        String s2 = new String("There");
        System.out.println(s1);
        s1=s2;
        System.out.println(s1);
        }
}
If Strings cannot be changed then s1 should still print out Hello, but if you try this snippet you will find that the second output is the string "There". What gives?

The immutability really refers to what the String reference points to. When s2 is assigned to s1 in the example, the String containing "Hello" in the String pool is no longer referenced and s1 now points to the same string as s2. The fact that the "Hello" string has not actually been modified is fairly theoretical as you can no longer "get at it".


The objective asks you to recognise the implications of the immutability of strings, and the main one seems to be that if you want to chop and change the contents of "strings" the StringBuffer class comes with more built in methods for the purpose.

Because concatenating string causes a new string to be instantiated "behind the scenes", there can be a performance overhead if you are manipulating large numbers of strings, such as reading in a large text file. Generally String immutability doesn't affect every day programming, but it will be questioned on the exam. Remember whatever round about way the question asks it, once created a String itself cannot be changed even if the reference to it is changed to point to some other String. This topic is linked to the way Strings are created in a "String pool", allowing identical strings to be re-used. This is covered in topic 5.2 as part of how the=operator and equals method acts when used with strings. Although neither the Java2 nor Java 1.1 objectives specifically mention it I am fairly confident that some questions require a knowledge of the StringBuffer class.





i was going through above explanation from following link

http://www.jchq.net/certkey/0802certkey.htm

It is not clear to me
>>>This topic is linked to the way Strings are created in a "String pool", allowing identical strings to be re-used.

Does it suposed to say Heap or Pool. What exactly immutability means.

Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
objectsCommented:
you not change the value of the string.
you're just changing which string the variable s1 references

yes it is supposed to ay pool

immutablity means the contents of the object (string) cannot be changed
0
 
gudii9Author Commented:
>>yes it is supposed to ay pool

is it because the objects in heap might be garbage collected later on references in heap remain same pointing to newly pointed object right. Is that is reason  references are immutable. please advise
0
 
for_yanCommented:
No it is correct that s1 prints There

Immutability means you don't change internal contents - sya, charcters within the string
But in this case s1 was in fact reference that was piointing to the place where "Hello" begins,
and s2 was pointing to the place where "There" begins
Now s1 = s2
means you put in memotry cell s1 the same pointer as was in pouinting to There
So s1 now has a pointer to "There"

No muatbiality of actual contents of the string is required for this
it is all just play with the pointers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
> is it because the objects in heap might be garbage collected later on references in heap remain same pointing to newly pointed object right. Is that is reason  references are immutable. please advise

its nothing to do with the heap.
It just makes implementation more efficiaent if you know that the value will never change
If you have a look at StringBuffer, its a mutable String class and has methods for changing the String value
0
 
Sathish David Kumar NCommented:
You cant change because string is final
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

 onces assign the value u cant reassign again ....  but you can change like ...

String fname="John";
fname+="David";  

now result is john David
0
 
gudii9Author Commented:
i still did not get immutability of string with respect to string buffer mutability. what can be changed what cannot be changed etc. what is string pool how is it different from stack and heap. please advise
0
 
for_yanCommented:

I'll repeat below my response to similar question, as it is as relevant here.

In addition here - about String pools

As you read about String immutablility - as is explained bit above and below -
the String is always kept as a whole - so when
you go from

String fname = "John"
to
fname = "David"

jave will not go into each cell of memory
and change char J to char D
but rather create new lline of cells haveing
"David" and reassign pointer of fname to this new line.
at the same time cells with "John" will live somewhere
and in some cases I guess virtual machine will for sometime
rememeber whaer they are and if soon enough it again needs
the String John it will not create a new line of memory cells with these charcaters,
but rather reuse the existing string "John" and just
get the pointer to that "old" string and reuse it -
that will save time populating other cells of memory
again


You certainly can change.

String fname = "John";

at this point you can use fname and its value will be John

System.out.println(fname); wikill print John

then somewhere down the code you say

fname = "David";

from this point on you'll use fname and

System.out.println(fname) will print David

What they are saying is about inner wortkings of it

In fact when you say fname = "David"
it wiill not go into the same cell locations in memory
and replace character J woth chracter D.

It would rather create in memory somewhere new string David
and change the pointer associated
with fname to point at this new location.

In most of the cases you as a programmer woul not care about
these detaails, it is enoght to know fro you, that fname now points to David,
 but in some situations
it is good to know about these inner workings

0
 
objectsCommented:
> i still did not get immutability of string with respect to string buffer mutability.

Have a look at the javadoc for the String class. There are no methods you can use to change the value of the string
If you now look at StringBuffer you will see it does have methods to change the String value

String is immutable (theres no way to change the string value), while StringBuffer is mutable.

No magic, its just to do with the operations available for the class


> what can be changed what cannot be changed etc. what is string pool how is it different from stack and heap. please advise

string pool is used internally by Java to improve performance and save memory. The idea is that if the same string is used many times we can save memory by only storing it once and have multiple instances of String use it. This can be done because String is immutable so we know the string value will never change.

Its really nothing to do with heap and stack.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now