There are many differences between String and StringBuffer. A list of differences between String and StringBuffer are given below:
No. | String | StringBuffer |
1) | String class is immutable. | StringBuffer class is mutable. |
2) | String is slow and consumes more memory when you concat too many strings because every time it creates new instance. | StringBuffer is fast and consumes less memory when you cancat strings. |
3) | String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. | StringBuffer class doesn't override the equals() method of Object class. |
Performance Test of String and StringBuffer
- public class ConcatTest{
- public static String concatWithString() {
- String t = "Java";
- for (int i=0; i<10000; i++){
- t = t + "Kundan";
- }
- return t;
- }
- public static String concatWithStringBuffer(){
- StringBuffer sb = new StringBuffer("Java");
- for (int i=0; i<10000; i++){
- sb.append("Kundan");
- }
- return sb.toString();
- }
- public static void main(String[] args){
- long startTime = System.currentTimeMillis();
- concatWithString();
- System.out.println("Time taken by Concating with String: "+(System.currentTimeMillis()-startTime)+"ms");
- startTime = System.currentTimeMillis();
- concatWithStringBuffer();
- System.out.println("Time taken by Concating with StringBuffer: "+(System.currentTimeMillis()-startTime)+"ms");
- }
- }
Time taken by Concating with String: 578ms
Time taken by Concating with StringBuffer: 0ms
String and StringBuffer HashCode Test
As you can see in the program given below, String returns new hashcode value when you concat string but StringBuffer returns same.
- public class InstanceTest{
- public static void main(String args[]){
- System.out.println("Hashcode test of String:");
- String str="java";
- System.out.println(str.hashCode());
- str=str+"Kundan";
- System.out.println(str.hashCode());
-
- System.out.println("Hashcode test of StringBuffer:");
- StringBuffer sb=new StringBuffer("java");
- System.out.println(sb.hashCode());
- sb.append("Kundan");
- System.out.println(sb.hashCode());
- }
- }
Hashcode test of String:
3254818
229541438
Hashcode test of StringBuffer:
118352462
118352462
Difference between StringBuffer and StringBuilder
There are many differences between StringBuffer and StringBuilder. A list of differences between StringBuffer and StringBuilder are given below:
No. | StringBuffer | StringBuilder |
1) | StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously. | StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously. |
2) | StringBuffer is less efficient than StringBuilder. | StringBuilder is more efficient than StringBuffer. |
StringBuffer Example
- public class BufferTest{
- public static void main(String[] args){
- StringBuffer buffer=new StringBuffer("hello");
- buffer.append("java");
- System.out.println(buffer);
- }
- }
StringBuilder Example
- public class BuilderTest{
- public static void main(String[] args){
- StringBuilder builder=new StringBuilder("hello");
- builder.append("java");
- System.out.println(builder);
- }
- }
Performance Test of StringBuffer and StringBuilder
Let's see the code to check the performance of StringBuffer and StringBuilder classes.
- public class ConcatTest{
- public static void main(String[] args){
- long startTime = System.currentTimeMillis();
- StringBuffer sb = new StringBuffer("Java");
- for (int i=0; i<10000; i++){
- sb.append("Kundan");
- }
- 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("Kundan");
- }
- System.out.println("Time taken by StringBuilder: " + (System.currentTimeMillis() - startTime) + "ms");
- }
- }
Time taken by StringBuffer: 16ms
Time taken by StringBuilder: 0ms
No comments:
Post a Comment