String Class in JAVA and their Immutable Property
# The String Class
This section covers the String class, and the key concept to understand is that once a String object is created, it can never be changed-so what is happening when a String object seems to be changing? Let`s find out.
# Keys Points:
> Each character is a16-bit Unicode character
> Strings are Objects in Java
> String Objects are immutable: Once a String object is created, it can never be changed
# Strings Are Immutable Objects
Let us create String Object
String s = new String();
This line of code creates a new object of class String, and assigns it to the reference variable s. So far, String objects seem just like other objects. Now, let`s give the String a value:
s = "abcdef";
As you might expect, the String class has about a zillion constructors, so you can use a more efficient shortcut:
String s = new String("abcdef");
And just because you`ll use strings all the time, you can even say this:
String s = "abcdef";
There are some subtle differences between these options that we`ll discuss later,but what they have in common is that they all create a new String object, with a value of "abcdef", and assign it to a reference variable s. Now let`s say that you want a second reference to the String object referred to by s:
String s2 = s; // refer s2 to the same String as s
So far so good. String objects seem to be behaving just like other objects, so what`s all the fuss about?.Immutability!
(What the heck is immutability?)
>>Once you have assigned a String a value, that value can never change- it`s immutable, frozen solid, won`t budge, fini, done.
The good news is that while the String object is immutable, its reference variable is not, so to continue with our previous example:
s = s.concat(" more stuff"); // the concat() method `appends`
// a literal to the end
Now wait just a minute, didn`t we just say that Strings were immutable? So what`s all this "appending to the end of the string" talk? Excellent question: let`s look at what really happened.
The VM took the value of String s (which was "abcdef"), and tacked " more stuff" onto the end, giving us the value "abcdef more stuff".
Since Strings are immutable, the VM couldn`t stuff this new value into the old String referenced by s, so it created a new String object, gave it the value "abcdef more stuff", and made s refer to it.
At this point in our example, we have two String objects: the
first one we created, with the value "abcdef", and the second one with the value "abcdef more stuff".
Technically there are now three String objects, because
the literal argument to concat, " more stuff", is itself a new String object. But we have references only to "abcdef" (referenced by s2) and "abcdef more stuff" (referenced by s).
What if we didn`t have the foresight or luck to create a second reference variable for the "abcdef" String before we called s = s.concat(" more stuff");? In that case, the original, unchanged String containing "abcdef" would still exist in memory, but it would be considered "lost."
No code in our program has any way to reference it-it is lost to us. Note, however, that the original "abcdef" String didn`t change (it can`t, remember, it`s immutable); only the reference variable s was changed, so that it would refer to a different String.
For more video tutorial please visit:
http://www.youtube.com/channel/UCI6PEB8D4Hfe9ZmfiX3JeOw
# The String Class
This section covers the String class, and the key concept to understand is that once a String object is created, it can never be changed-so what is happening when a String object seems to be changing? Let`s find out.
# Keys Points:
> Each character is a16-bit Unicode character
> Strings are Objects in Java
> String Objects are immutable: Once a String object is created, it can never be changed
# Strings Are Immutable Objects
Let us create String Object
String s = new String();
This line of code creates a new object of class String, and assigns it to the reference variable s. So far, String objects seem just like other objects. Now, let`s give the String a value:
s = "abcdef";
As you might expect, the String class has about a zillion constructors, so you can use a more efficient shortcut:
String s = new String("abcdef");
And just because you`ll use strings all the time, you can even say this:
String s = "abcdef";
There are some subtle differences between these options that we`ll discuss later,but what they have in common is that they all create a new String object, with a value of "abcdef", and assign it to a reference variable s. Now let`s say that you want a second reference to the String object referred to by s:
String s2 = s; // refer s2 to the same String as s
So far so good. String objects seem to be behaving just like other objects, so what`s all the fuss about?.Immutability!
(What the heck is immutability?)
>>Once you have assigned a String a value, that value can never change- it`s immutable, frozen solid, won`t budge, fini, done.
The good news is that while the String object is immutable, its reference variable is not, so to continue with our previous example:
s = s.concat(" more stuff"); // the concat() method `appends`
// a literal to the end
Now wait just a minute, didn`t we just say that Strings were immutable? So what`s all this "appending to the end of the string" talk? Excellent question: let`s look at what really happened.
The VM took the value of String s (which was "abcdef"), and tacked " more stuff" onto the end, giving us the value "abcdef more stuff".
Since Strings are immutable, the VM couldn`t stuff this new value into the old String referenced by s, so it created a new String object, gave it the value "abcdef more stuff", and made s refer to it.
At this point in our example, we have two String objects: the
first one we created, with the value "abcdef", and the second one with the value "abcdef more stuff".
Technically there are now three String objects, because
the literal argument to concat, " more stuff", is itself a new String object. But we have references only to "abcdef" (referenced by s2) and "abcdef more stuff" (referenced by s).
What if we didn`t have the foresight or luck to create a second reference variable for the "abcdef" String before we called s = s.concat(" more stuff");? In that case, the original, unchanged String containing "abcdef" would still exist in memory, but it would be considered "lost."
No code in our program has any way to reference it-it is lost to us. Note, however, that the original "abcdef" String didn`t change (it can`t, remember, it`s immutable); only the reference variable s was changed, so that it would refer to a different String.
For more video tutorial please visit:
http://www.youtube.com/channel/UCI6PEB8D4Hfe9ZmfiX3JeOw
No comments:
Post a Comment