Image for Buffer Builder standard String

Buffer Builder standard String

In this blog post I will write about differences between String, StringBuffer and StringBuider in Java. While similar, there are som major difference. In the picture below you will see the differences at a glance, but read on to get a clearer picture.

Differences of a few String classes. String is immutable, StringBuffer is multi-thread mutable and StringBuilder is single-threaded mutable.
This is a first post of several that I will write about Java development.

String


String is one of the first objects a Java newbie will encounter. On the surface it acts as the primitives but it is not and its name gives a hint of this. String with a capital S! So a String can be instantiated like a primitive or an object:


String name = "Anders";
String anotherName = new String("Tibertius");


Writing the long version is a bit superfluous so usually we stick with creating Strings as if they were a primitive datatype.

When we write something like this:


String hobby = "running";
hobby = "gaming";


It means we are actually creating two String objects! The reason is that Strings are constant, so rather than updating a value within the object Java creates a new object.

What's nice of having String as an object is that we also get a bunch of methods. We can convert a string of letters to uppercase or lowercase and also get the length of the string, only by calling the method on the String variable.


System.out.println(anotherName.toUpperCase()); // 9
System.out.println(anotherName.length()); // TIBERTIUS


What we have learned so far is that while we often treat Strings as primitive values, concatenating or reassigning values on a String object actually means that we are creating new objects. Often enough this is not a problem.

Learn more about String on the official documentation page.

StringBuffer


StringBuffer is like a String, only that it is obviously an object! But more importantly: it is mutable. This means that its value can change as well as its length, so no more Ms Constant here!


StringBuffer mrFantastic = new StringBuffer("Mr Fantastic");
mrFantastic.append(" is really COOL!");
System.out.println(mrFantastic); //Mr Fantastic is really COOL!

StringBuffer is obviously an object as the interactions with it requires us to use its methods: concatination needs us to use the append-method.

And lastly, StringBuffer is made to be thread safe! This means that a StringBuffer object will be updated in a seemingly sequential order when needed.

Learn more about StringBuffer on the official documentation page.

StringBuilder


StringBuilder is like StringBuffer but with a major difference. It is not threadsafe. It has the same methods and is also a mutable object. The advantage of a StringBuilder object when compared to StringBuffer is that it is faster. It should therefore be used whenever a mutable string is needed and it is run off of a single thread (which usually is the case).
Learn more about StringBuffer on the official documentation page.

In the next blog post I will write about packages in Java. See you Wednesday!