Monday, 20 April 2015

String, String Buffer, String Builder

I would highly recommend that you read my previous post about String immutability in order to understand String immutability. This will help you to understand when and where to use String, StringBuffer or StringBuilder.

As explained in my previous post, String objects are immutable i.e. once an object is created with some value, its value can never be changed. If any manipulation is done to that string, a whole new String object is created while the old one remains in the memory. This may result in wastage of precious memory. So, if large number of operations are done on a string, a large number of objects will be created in memory(one object per operation). Those String objects will be of no use and will result in wastage of memory, which is a very precious resource.

It is where the StringBuffer and StringBuilder come into play. Any number of operations can be performed on a single StringBuffer or StringBuilder object. The changes will be reflected in that single object only unlike a String object in which a new String object is created for every operation performed on a string.
  • String name="xyz";
  • name.toUpperCase();
     
    The new String object is created and instantly lost as no reference variable is referencing to it.(Wastage of memory)

  • name=name.toUpperCase();
    The old string object "xyz" is lost now as the reference variable 'name' is now referencing to new String object.(Wastage of memory)

Sunday, 19 April 2015

String, String Immutability, String Constant Pool

String 

String is usually created in following ways:

String name="xyz";
or
String name=new String("xyz");
or
char[] n={'x','y','z'};
String name=new String(n); 

There are many other constructors of String class taking arguments of different kinds.

Difference between String name="xyz" and String name=new String("xyz")

 

What does String name="xyz" mean?

 

Every object that is created in Java is created on Heap. "xyz" is a String object created on heap and 'name' is the reference variable refering to that String object.


Using this way of declaring string, the String object created is added to special area of memory in heap called the "String constant pool". JVM set aside this special area of memory to make Java more memory efficient. How? First, let us understand what is String immutability.

String are Immutable Objects

 String s=name;     //create a 2nd reference variable refering to the same string 

      
    s=s.concat("abc");
 

From the above figure we can see that a new String object("xyzabc") is created and reference variable s point towards this newly created object. An object of the string concatenated("abc") is also created but it is not referenced by any variable.

A whole new String object(xyzabc) is created after concatenation and the old object(xyz) is not changed. This is known as String Immutability i.e. a String value can never be changed.

Let's look at another example:

     String x="Java";
     x.concat(" Rocks!");
  System.out.println("x= "+x);     //the output is "x=Java"
  • First line created a new String object having value "Java" and refer x to it.
  • Second line created a new String object having value "Java Rocks!", but nothing refers to it. This String Object is instantly lost.
  • So, the output is "Java" as reference varibale x refers to this String object only.