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.

 

 

13 Replies to “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.

  2. Thank you for every one of your labor on this web site. My daughter really loves making time for investigations and it’s really obvious why. Most people learn all regarding the lively mode you present reliable tactics through this web site and even inspire participation from some others on the point then our favorite princess is really starting to learn a whole lot. Take advantage of the rest of the year. You are always doing a remarkable job.

  3. I am only writing to make you understand what a superb experience our princess undergone viewing your webblog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal coaching heart to get many more easily fully understand certain complicated matters. You actually did more than my desires. I appreciate you for delivering such great, healthy, educational and also cool guidance on that topic to Evelyn.

  4. I enjoy you because of all of your effort on this blog. Debby enjoys conducting internet research and it’s easy to see why. A number of us hear all about the compelling manner you create efficient ideas by means of the blog and boost contribution from other ones about this article and my child is being taught a great deal. Enjoy the remaining portion of the new year. You’re carrying out a fantastic job.

  5. I am just writing to make you be aware of of the beneficial discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get certain people completely comprehend a number of complex issues. You actually did more than her desires. I appreciate you for coming up with such effective, dependable, edifying and cool guidance on that topic to Emily.

  6. My husband and i felt now fortunate Emmanuel could conclude his investigation from your ideas he gained in your site. It’s not at all simplistic just to choose to be giving for free information that most people have been making money from. And we all do understand we have the website owner to be grateful to because of that. The explanations you’ve made, the easy site menu, the friendships you can give support to engender – it’s everything extraordinary, and it’s facilitating our son and our family feel that that idea is enjoyable, and that is highly important. Many thanks for all the pieces!

  7. I’m commenting to let you be aware of of the amazing discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such beneficial, dependable, edifying and cool guidance on that topic to Emily.

  8. Thanks for the useful information on credit repair on your web-site. The thing I would offer as advice to people will be to give up a mentality they will buy at this moment and pay back later. As being a society most people tend to repeat this for many factors. This includes family vacations, furniture, plus items we really want to have. However, you must separate a person’s wants from the needs. When you’re working to fix your credit score you really have to make some trade-offs. For example you possibly can shop online to economize or you can click on second hand suppliers instead of highly-priced department stores to get clothing.

Leave a Reply

Your email address will not be published.