Tuesday, 9 February 2016

String, StringBuffer & StringBuilder in JAVA

String:
      A string is a sequence of characters. A string can creates in JAVA in two ways
1.       By using ‘new’

In this way of creation, two strings will be created one is in heap memory and second one is in Srting Constant Pool (This is one of the memories) if the string is not present

                            Ex: String s = new String(“Raju”);
                             ‘s’  will be created in Heap Memory and “Raju” (it’s a String Constant)  will be created in String Constant pool if the constant is not present otherwise it will point to already created constant.

2.       By using string constant

In this way of creation, String will created in Srting Constant Pool

               Ex: String s= “Raju”;
              public class StringDemo {
      
              public static void main(String[] args) {
                     // Creation of a string using a new operator
                     String s= new String("Raju");
                     // Creation of a string using a constant
                     String s1 = "Raju";
                     String s2 = "Raju";
                    // equals method in String class is over ridden from Object class for content comparison
                     System.out.println(s1.equals(s2)); // true because content is same
                     System.out.println(s.equals(s2)); // true because content is same
             
                     // == operator is used for address comparison
                     System.out.println(s==s1); // false Reason: s will created on Heap memory s1 in String Constant Pool
                     System.out.println(s1==s2);// true Reason: s1 is already created in SCP hence s2 will point to s1
              }
}
Important points:
1.       String is immutable. Once we created the string  then it won’t change even we concatenated with other string
   Ex: String s=new String(“Raju”);
           String s1= new String(“JAVA trainer”);
                                  s.concat(s1);
                             Here in total 3 strings will creates
                                      Raju, JAVA trainer, Raju JAVA trainer – s will points this after concatenation
 StringBuffer and StringBuilder:
The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.
Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again without leaving behind a lot of new unused objects.
The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilders methods are not thread safe (not Synchronised).
It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.

Example: StringBuffer
public class Test{
    public static void main(String args[]){
       StringBuffer sBuffer = new StringBuffer(" test");
       sBuffer.append(" String Buffer");
       System.out.println(sBuffer); 
   }
}
Example: StringBuilder
public class Test{
    public static void main(String args[]){
       StringBuilder sBuilder = new StringBuilder(" test");
       sBuffer.append(" String Buffer");
       System.out.println(sBuffer); 
   }
}

Important Point:  Both StringBuffer and StringBuilder are mutable 

No comments:

Post a Comment