Differences between String and StringBuffer and StringBuilder.

We are going to discuss the difference between String and StringBuffer and StringBuilder.

1.Differences between String and StringBuffer.

String: Once we create a string object we can’t perform any changes in the existing object. If we try to perform any changes with those changes a new object will be created.  This non-changeable is nothing but the immutability of the string object. we can’t change the sting object once we create.

  1. String class is immutable, which means it can’t change.
  2. It is slow, because every time it creates new instance, so it is consuming more memory when you concat too many strings.
  3. String class overrides the equals() method of Object class.

 

Programing Example of String.

package com.technocalRound;

class Example {
public static void main(String args[]) {
String s = "Niraj";
s.concat(" Singh");// concat() method is appends the string at the end of existing object
System.out.println(s);// It will print Niraj
// because strings are immutable objects that's why it not appends
}
}

Output:

Niraj

 

StringBuffer: Once we create a StringBuffer object we can perform any type of changes in the existing object.
This changeable is nothing but the mutability of the string object. We can change the StringBuffer object after the creation of the object.

  1. StringBuffer class is mutable, which means it can change.
  2. It is fast and consuming less memory when you cancat strings.
  3. StringBuffer class doesn’t override the equals() method of Object class.

Programing Example of StringBuffer.

package com.technocalRound;

class Example {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Niraj");
sb.append(" Singh");// append() method is appends the StringBuffer at the end of existing object
System.out.println(sb);// It will print Niraj Singh
// because StringBuffer are mutable objects that's why it appends
}
}

Output:

Niraj Singh

 

2.Differences between StringBuffer and StringBuilder.

StringBuilder is exactly the StringBuffer (including Method and Constructors) except the following differences.

StringBuffer:

  1. Every method present in StringBuffer is synchronized.
  2. At a time only one thread is allowed to operate on StringBuffer object. Hence StringBuffer object is Thread safe.
  3. It increases waiting time of threads and hence relatively perforation is slow.
  4. Introduced in java 1.2 version.

 

StringBuilder:

  1. Every method present in StringBuffer is synchronized.
  2. At a time multiple threads are allowed to operate on StringBuilder object. Hence StringBuffer object is not thread-safe.
  3. Threads are not required to wait to operate on StringBuilderec and hence relatively perforation is high.
  4. Introduced in Java 1.5 version.

 

Programing Example of StringBuilder.

package com.technocalRound;

class Example {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("Niraj");
sb.append(" Singh");// append() method is appends the StringBuffer at the end of existing object
System.out.println(sb);// It will print Niraj Singh
// because StringBuffer are mutable objects that's why it appends
}
}

Output:

Niraj Singh

 

When to use String, StringBuffer, and StringBuilder?

  1. If the content is fixed, and won’t change frequently then we should go for String.
  2. when the content is not fixed and keep on changing but thread-safe is required, then we should go for StringBuffer.
  3. If the content is not fixed and keeps on changing but thread-safe is not required, then we should go for StringBuffer.

 

Programming Example of String, StringBuffer, and StringBuilder.

package com.technocalRound;

public class Example{ 
public static String concatWithString() { 
String t = "Java"; 
for (int i=0; i<10000; i++){ 
t = t + "TechnicalRound"; 
} 
return t; 
} 
public static String concatWithStringBuffer(){ 
StringBuffer sb = new StringBuffer("Java"); 
for (int i=0; i<10000; i++){ 
sb.append("TechnicalRound"); 
} 
return sb.toString(); 
} 
public static String concatWithStringBuilder(){ 
StringBuilder sb = new StringBuilder("Java"); 
for (int i=0; i<10000; i++){ 
sb.append("TechnicalRound"); 
} 
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");
startTime = System.currentTimeMillis(); 
concatWithStringBuilder(); 
System.out.println("Time taken by Concating with StringBuilder: "+(System.currentTimeMillis()-startTime)+"ms"); 
} 
}

Output:

Time taken by Concating with String: 1665ms
Time taken by Concating with StringBuffer: 2ms
Time taken by Concating with StringBuilder: 1ms

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

 

One Reply on “Differences between String and StringBuffer and StringBuilder.”

  1. hey there and thank you for your information – I’ve
    certainly picked up something new from right here.

    I did however expertise some technical points using this
    website, as I experienced to reload the site many times previous
    to I could get it to load properly. I had been wondering
    if your hosting is OK? Not that I’m complaining, but slow loading instances times will sometimes affect your placement in google
    and could damage your quality score if ads and marketing with Adwords.
    Well I am adding this RSS to my e-mail and can look out for a lot more of your respective exciting content.

    Ensure that you update this again soon.

Leave a Reply

Your email address will not be published. Required fields are marked *