The String



In java String are objects.Just like other objects, we can create an instance of a String with the new Keyword ,as follows

String s=new String();

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";

Strings are immutable , means Once you have assigned a String value that value can never change. The good news
is that while the String object is immutable, its reference variable is not,
lets discussion start....

String s="abcdef";
// create a new string object ,with value "abcdef",refer s to it
String s2 = s;
// create a 2nd reference variable refferring to the same String

S = s.concat("more stuff");
// 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. ( R emember s2 is still referring
to the original "abcdef" String.)

The figure will give detailed explaination:

No comments:

Post a Comment