Sunday, June 7, 2015

String Tutorials


String 



As the name suggests, String Pool is a pool of Strings stored in Java Heap Memory. We know that String is special class in java and we can create String object using new operator as well as providing values in double quotes.







Example Program:


package StringProgram;

public class Immutable {

public static void main(String[] args) { 
String a="welcome";
String b="welcome";
String c=new String("welcome");
System.out.println(a==c);
System.out.println(a==b);

}

}



Output:

false
true



example :

package StringProgram;

public class Immutable {

public static void main(String[] args) { 
String a="we"+"lcome";
String b="welcome";
String d="wel"+"co"+"me";
String e=new String("wel"+"co"+"me");
String c=new String("welcome");
System.out.println(a==b);
System.out.println(d==e);
System.out.println(a.equals(b));
System.out.println(d.equals(e));
System.out.println(d.equals(a));
System.out.println(d.equals(c));
System.out.println();

}

}
output :

true
false
true
true
true
true


Java StringBuffer class



Java StringBuffer class is used to created mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.



Important Constructors of StringBuffer class

  1. StringBuffer(): creates an empty string buffer with the initial capacity of 16.
  2. StringBuffer(String str): creates a string buffer with the specified string.
  3. StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.



package StringProgram;


public class StringBuffer1 {

public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
//sb.append( " welcome");
//sb.insert(5, "welcome");
//sb.replace(5,7,"mkk");
//sb.delete(5,7);
System.out.println(sb.capacity());
sb.append("12345678910111213jjjj");
System.out.println(sb.capacity());
sb.append("ksdncidsnvdbucsducsdc8ecbewnc");
System.out.println(sb.capacity());
sb.append("manilkdscnlskjnkwlxknlksannansxan");
System.out.println(sb.capacity());

}

}
output :

16
34
70
142







Java StringBuilder class


Java StringBuilder class is used to create mutable (modifiable) string. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.



Important Constructors of StringBuilder class

  1. StringBuilder(): creates an empty string Builder with the initial capacity of 16.
  2. StringBuilder(String str): creates a string Builder with the specified string.
  3. StringBuilder(int length): creates an empty string Builder with the specified capacity as length.


Performance Test of String and StringBuffer


package StringProgram;

public class StringAndStringBuffer {
public static String Strings(){
String t="java";
for(int i=0;i<10000;i++){
t=t+"tutorials";
}
return t;
}
public static String StringBuffers(){
StringBuffer sb=new StringBuffer("java");
for(int i=0;i<10000;i++){
sb.append("to android");
}
return sb.toString();
}

public static void main(String[] args) {
long starttime=System.currentTimeMillis();
Strings();
System.out.println("String :"+(System.currentTimeMillis()-starttime)+"ms");
starttime=System.currentTimeMillis();
StringBuffers();
System.out.println("StringBuffer :"+(System.currentTimeMillis()-starttime)+"ms");
}

}
output:
String :710ms
StringBuffer :0ms


String and StringBuffer HashCode Test

package StringProgram;

public class Sgtirn {

public static void main(String[] args) {
       System.out.println("Hashcode test of String:");  
       String str="java";  
       System.out.println(str.hashCode());  
       str=str+"tpoint";  
       System.out.println(str.hashCode());  
  
       System.out.println("Hashcode test of StringBuffer:");  
       StringBuffer sb=new StringBuffer("java");  
       System.out.println(sb.hashCode());  
       sb.append("tpoint");  
       System.out.println(sb.hashCode());  
  }

}
output:

Hashcode test of String:
3254818
229541438
Hashcode test of StringBuffer:
27134973
27134973


package StringProgram;

public class StringBufferAndStringBuilder {
public static void main(String[] args) {

long startTime = System.currentTimeMillis();  
        StringBuffer sb = new StringBuffer("Java");  
        for (int i=0; i<10000; i++){  
            sb.append("Tpoint");  
        }  
        System.out.println("Time taken by StringBuffer: " + (System.currentTimeMillis() - startTime) + "ms");  
        startTime = System.currentTimeMillis();  
        StringBuilder sb2 = new StringBuilder("Java");  
        for (int i=0; i<10000; i++){  
            sb2.append("Tpoint");  
        }  
        System.out.println("Time taken by StringBuilder: " + (System.currentTimeMillis() - startTime) + "ms");
}}
 


output :


Time taken by StringBuffer: 16ms
Time taken by StringBuilder: 0ms

Difference between String, StringBuffer and StringBuilder 




----------------------------------------------------------------------------------
                          String                    StringBuffer         StringBuilder
----------------------------------------------------------------------------------                 
Storage Area | Constant String Pool           Heap            Heap 
Modifiable     |  No (immutable)            Yes( mutable )   Yes( mutable )
Thread Safe  |           Yes                         Yes                   No
Performance |         Fast                        Very slow           Fast
-----------------------------------------------------------------------------------