Descibe the significance of the immutability of String objects

The theory of the immutability of the String class says that once created, a
string can never be changed. Real life experience with Java programming
implies that this is not true.

Take the following code

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 theorectical 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.
Credits to: http://www.jchq.net/tutorial/09_02Tut.htm


On Tue, Aug 30, 2011 at 3:07 PM, VM <veer.mucha...@gmail.com> wrote:

> Why is String considered immutable while the code below works?
>
> String name = "Sang"; System.out.println(name);
> name ="Shin";
> System.out.println(name);
>
> --
> To post to this group, send email to
> javaprogrammingwithpassion@googlegroups.com
> To unsubscribe from this group, send email to
> javaprogrammingwithpassion+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en




-- 
Trey Haertel

-- 
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to