Table of Contents
Table of Contents
Let us do a performance testing of String class and String Buffer Class and see what the result is. We have taken objects of both String class and String Buffer Class, than we have appended String value= “Android” to both for same time period, and checked time taken.
public class PerformanceTesting{
public static String concatinateString(){
String string = "Abhi";
for (int i=0; i<10000; i++){
string = string + "Android";
}
return string;
}
public static String concatinateStringBuffer(){
StringBuffer stringbuffer = new StringBuffer("Abhi");
for (int i=0; i<10000; i++){
stringbuffer.append("Android");
}
return stringbuffer.toString();
}
public static void main(String[] args){
long startTime = System.currentTimeMillis();
concatinateString();
System.out.println("Time taken for Concatination with String: "+(System.currentTimeMillis()-startTime)+"ms");
startTime = System.currentTimeMillis();
concatinateStringBuffer();
System.out.println("Time taken for Concatination with StringBuffer: "+(System.currentTimeMillis()-startTime)+"ms");
}
}
Output:
Time taken for Concatination with String: 465ms Time taken for Concatination with StringBuffer: 1ms
From output as shown above it is clear that String class takes more time than String Buffer class.
Let us do Hash Code testing of String class and String Buffer Class and see what the result is. We have taken objects of both String class and String Buffer Class, than we have appended String value= “Android” to both objects. As shown in the following program.
public class HashCodeTesting{
public static void main(String args[]){
System.out.println("Hashcode testing of String:");
String string="Abhi";
System.out.println(string.hashCode());
string=string+"Android";
System.out.println(string.hashCode());
System.out.println("Hashcode testing of StringBuffer:");
StringBuffer stringbuffer=new StringBuffer("Abhi");
System.out.println(stringbuffer.hashCode());
stringbuffer.append("Android");
System.out.println(stringbuffer.hashCode());
}
}
Output:
Hashcode testing of String: 2033922 1496841997 Hashcode testing of StringBuffer: 31168322 31168322
From output it is clear that hashcode of String after appending is changed, whereas hashcode of String Buffer remains same after appending another string.
Premium Project Source Code:
Hi
Please can you explain and put an example code for internationalization in java?
Thanks a lot