Benefits of StringBuffer and StringBuilder in java

The StringBuffer class since java 1.0 should be replaced with the newer StringBuilder class in java 1.5 if you don’t need any synchronization. What I learned in college about StringBuffer which probably is the same with StringBuilder is that it is a lot more efficient when concatenating Strings.

For example,

String temp = “yo”;

yo += ” yo”;

Now you would think it would just append the second ” yo” to the end of temp, but what happens is that a new string is created and then everything is copied into it. This gets messy if you put the yo += “yo”; in a loop.

For StringBuilder and StringBuffer, it will just append the ” yo” to the StringBuilder object in a faster and space efficient setting. If you hit the end of the size of the StringBuilder, it’ll just increase the size automatically.

This entry was posted in software and tagged , , . Bookmark the permalink.

One Response to Benefits of StringBuffer and StringBuilder in java

  1. Here’s the stackoverflow article I saw:

    It basically says that if you compile yo += “yo” and disassemble it…it’s the same thing as the StringBuilder method.

Leave a Reply

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