String in java


The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.


Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. 
For example:


     String str = "abc";
 is equivalent to:
     char data[] = {'a', 'b', 'c'};
     String str = new String(data);


Creating String as new () and literal   (Difference between creating String as new () and literal)

When we say new on String, a new instance of String will be created always while when we assign a string to a String reference a new String object will be created only if that string is not present in the String pool. To understand this, have a look at the following example :


1:String a = "abc";
2:String b = new String("abc");
3:String c = a;

No of objects in the pool = 2.

Step 1 : will check whether "abc" instance avalable in the pool or not ; this step there is no instance of "abc", so new instance of "abc" will creat in string pool.
Step 2 : new on String always will create new instance.
Step 3 : will check whether "abc" instance avalable in the pool or not ; this step there is instance exist for "abc". so the instance of reference will change to c.

So total 2 objects exist in string pool.

Immutable object and mutable object

immutable : Immutable objects can be used to define values or attributes that you don't want to be changed.

we can consider the string object as immutable objects, because we can't alter the content of the string object.

ex : 
String imm = "abc";
imm = "abc"+"d";



here we are not adding the value to exist object ("abc"),  
1)"d" will add to "abc",
2) will create new object "abcd" 
3) the reference of "abc" will map to "abcd";

Mutable object: We can change/alter the value of the object.
ex :
StringBuffer mut = new StringBuffer("abc");
mut.append("d");

Here the value of "mut" changing directly. 

Problem with String
Many a times we create a String and then perform a lot of operation on them e.g. converting string into uppercase, lowercase , getting substring out of it , concatenating with other string etc. Since String is an immutable class every time a new String is created and older one is discarded which creates lots of temporary garbage in heap.  If String are created using String literal they remain in String pool.

String and StringBuffer and StringBuilder
  • String is immutable while StringBuffer is mutable means you can modify a StringBuffer object once you created it without creating any new object. 
  • StringBuffer is very good with mutable String but it has one disadvantage all its public methods are synchronized which makes it thread-safe but same time slow.
  • In JDK 5 they provided similar class called StringBuilder in Java which is a copy of StringBuffer but without synchronization.









No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...