O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Better Efficiency with StringBuffers

The StringBuffer class is useful for making strings that need to be concatenated, appended to, or otherwise tangled with more than two or three times. Because regular String objects are immutable, changing a String actually creates a whole new String, which is an expensive operation. To make String-changing more efficient, we use StringBuffer. If you've done any work in C#, you'll recognize this as System.Text.StringBuilder.

There are generally only two things you do with a StringBuffer: append text to it, and then, when you're all done appending, call the toString() method to use it as a String. Like so:

 package net.javagarage.demo.String; public class StringBufferDemo { public static void main(String[] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required