20 July 2012

String Class

String Class

Hello Friends, Nice to see you again.
After lots of post related to Java Swing, Here, I come with core java part.

String is the class which is mostly used in every java based applications.
We can say that java applications can not be imagined without the String Class.... :)

Here we will discuss the core feature of String class.
So sit tight for unforgoettable journey of Java String Class....

Strings Are Immutable Objects:

In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits (not the skimpy 7 or 8 bits that ASCII provides), a rich, international set of characters is easily represented in Unicode.

In Java, strings are objects. Just like other objects, you can create an instance of a String with the new keyword, as follows:
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";

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

Here you feel nothing special right..? But the word Immutability! comes here in play
(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 part is that though the String object is immutable, its reference variable is not.

Lets understand with the example:

s = "abcdef";
s = s.concat(" more stuff");  // the concat() method 'appends'
               // a literal to the end

Here as we said earlier, String is immutable, then what the heck is this --- appending old string..!
This is the tricky part of String Class. Here please pay attention how VM behaves...!

First ,the value s contains was "abcdef", then we append it and now VM add " more stuff" at the end.
Now new String will be ""abcdef more stuff".

Now  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".

Here ehat happened....we assigned s to s2 earlier then we append s with " more stuff".
If we did not assign s to s2 then after append s the old String "abcdef" would be lost in memory...
Surprise...! This is because when you append the old String object with adding new String at the end of it. It will create another new String with old String + new appended String.
And the older value will be lost in memory without any reference variable. 
Look the below image for better understanding of working of String in Memory...

The above figure shows what happens on the heap when you reassign a reference variable. Note that the dashed line indicates a deleted reference.

To review our first example:

String s = "abcdef"; // create a new String object, with value "abcdef", refer s to it
String s2 = s; // create a 2nd reference variable referring to the same String
// create a new String object, with value "abcdef more stuff",
// refer s to it. (Change s's reference from the old String to the new String.) 
//( Remember s2 is still  referring to  the original "abcdef" String.)
// s = s.concat(" more stuff");

So this is how String is really Immutable....

Note: Please do not use String in you code where too much updation and modification in String is required...In thiat case use StringBuilder Class.


So I hope this post is helpful in understanding Of String Class.

Feel free to ask me if you have any dought regarding this.
You can also give me suggessions to improve this post.

Leave comments if you like this post...!

Thank you.
Nirav Raval