Java Tutorials – String, StringBuffer and StringBuilder in Java

Hello people…! This is a new post in Java Tutorials – String, StringBuffer and StringBuilder in Java. In this post, I will talk about some basics about Strings in Java. Why basics…? Well, there is a lot in Java when it comes to Strings, which we can’t understand right now due to lack of knowledge. But, on the other hand, these are so basic elements of simple programs, that we can hardly write any useful code without these. So, I will cover only what is required to programs involving good computation, so that you will be able to practice Java writing those programs which you wrote to practise C.

Strings in Java

First of all, to learn Strings in Java… You must forget whatever you learnt about strings in C…! This is because, a String in Java is not exactly treated as an array of characters terminated by a null character..! No..! String in Java is actually an object of the String Class, which is in the java.lang package, or library. But, we can use it fairly as we use strings in C++ (#include <string>). If you know only C, then watch closely..!

String name = "Albus Dumbledore";

This is how we create a String in Java. Here, the variable name, is called an Object Reference. As the name suggests, object reference, it points to an object. The object reference here is of the data type String, which is a class. We will talk a lot about a class later, but for now, think of it as a struct where we can have methods, hence, a user defined data type, here defined by Java for you. Now, the variable name is now pointing to an object of String Class, which has the data “Albus Dumbledore”.

So, when the above line is executed, we don’t get an array of characters, but instead, we get one chunk of memory where the string is stored.

Now, how would we access the variables inside a struct..? We would use the dot operator (struct.variable). Similarly, we use the dot operator, to access the variables and methods of the String Class. Well, let’s proceed with a familiar example of a C struct.

This is how we would create a variable of type struct Person and use its members. In the same fashion, we will use the members of String Class, but this time, it will be functions, not variables (not that we can’t have variables)..!

As you can see, when we use the object reference, name or plan, we can access the various functions provided to us by String Class. All these methods are accessed by the object reference. I’ve used a lot of functions in the program, let’s look at them in a little more detail –

length() – This function returns an int, which is the length of the string –

System.out.println(name.length());

But, which string…? The one name is pointing to..! If the same function were called on another object reference, it would return the length of that string, as in –

The return type of this function is boolean, so, it returns true if the prefix or suffix is present, otherwise, false.

These were just a few of the many functions that Java provides. We will have a brief note about a few more of others later.

Basic Operations on Strings in Java

Concatenation – We use the addition operator, ‘+’ to concatenate Strings in Java. This is much like the modern programming languages. String Class does provide a method, but this is the most compact way of concatenating strings.

Equality – Beginners make a common mistake of comparing two strings using “==”. This does not work properly..! We must always use the equals() method of the String class to compare two strings. The data type of the argument is something I’ll reserve for later..! But, remember that this is how we compare Strings –

Less than zero – If the argument is lexicographically smaller than the string in object reference.

Equal to zero – If both strings are lexicographically equal.

Greater than zero – If the argument is lexicographically greater than the string in object reference.

Mutable Strings

Strings in Java are actually immutable in nature. It means that, they cannot be modified or, they are read-only. We cannot change the content inside them. Then how can we create strings where we can actually manipulate them..? We can do so, by creating objects of –

StringBuffer Class

StringBuilder Class

Till now we have created strings using the String Class. But as they are immutable, we will create strings using the other classes. Let’s use the StringBuffer Class –

As you can see, these strings can be modified. We use the various methods provided in the StringBuffer Class to manipulate them. Some of them are discussed in the next section.

Methods in StringBuffer Class

Method Signature

Short Description

StringBuffer append(String str)

Appends, adds it to the end, of the argument string.

char charAt(int index)

Retrieves the character at the index in the range [0, length – 1]

StringBuffer delete(int startIndex, int endIndex)

Deletes the characters from the string in the range [startIndex, endIndex)

StringBuffer deleteCharAt(int index)

Deletes the character from the string at the specified index.

int indexOf(String str)

If found, returns the index of the first occurrence of the string str, otherwise, returns -1.

StringBuffer insert(int offset, String str)

Inserts the string str at an offset, or, inserts it in between characters at index – (offset – 1) and offset

int lastIndexOf(String str, int startIndex)

If found, returns the index last occurrence of str from startIndex (optional). Call lastIndexOf(“”) returns the string length.

int length()

Returns the length of the string.

StringBuffer replace(int startIndex, int endIndex, String str)

Replaces the [startIndex, endIndex) portion of the string with str.

StringBuffer reverse()

Returns the string with the character sequence reversed.

void setCharAt(int index, int newChar)

Replaces the character at index to newChar

String substring(int startIndex, int endIndex)

Returns an object of String Class which is a substring of the original string in the range [startIndex, endIndex). The parameter endIndex is optional.

Difference between StringBuffer and StringBuilder

The same methods which are provided by the StringBuffer Class are also provided by the StringBuilder Class (of course they differ in the return types), but with a small twist. Well, you need to know a little Multi-threading to understand this properly, but it doesn’t harm to learn them now. We will re-visit this topic again, but for now, I wan’t you to be ready if anybody asks the difference.

StringBuffer

StringBuilder

All methods in StringBuffer are synchronized. Which means that they are thread-safe. By thread-safe, we mean that no two threads can simultaneously access the method of an object of StringBuffer Class.
StringBuffer Append in Java

Methods are not synchronized. Which means that they are not thread-safe. So, using StringBuilder in a multi-threading environment can cause data corruption due to concurrency.
StringBuilder Append in Java

Being synchronized, they are slow. This is because of the thread safety.

Faster than StringBuffer because they don’t carry a burden on synchronization.

But, in a single-threading environment, these two should deliver fairly equal performance. So this choice is left to the programmer. The reference snips are taken from the original java.lang.StringBuffer and java.lang.StringBuilder Class provided by Java, as viewed from NetBeans IDE. I don’t want you to worry about what you don’t understand. All I want you to notice in those snips is the presence and absence of the keyword synchronized. What does it do..? We’ll get to it later.

Behind the Curtains !

This is an advanced discussion about the internals of String, StringBuilder, StringBuffer class. Beginners… Feel free to skip this….!

Well, so far we talked about Strings as an object, a chunk of memory, not as an array of characters. But the thing is that, internally, String is an array of characters. In the String class, we have a member which is an array of characters. And this member is a constant for the String Class. So, its like…. If we put this in C terms, it would look like –

struct String
{
const char value[10];
};

Array in String Class of Java

But it is not as simple as it looks, arrays function differently in Java. In fact, if Oracle sees me comparing String Class with that piece of C code, it might say, “If you delete that now, that’ll be the end of it. I will not look for you, I will not pursue you. But if you don’t, I will look for you, I will find you, and I will kill you.” 😛
To get an idea, this is what happening in the String Class –
As you can see –

There’s an array of name value.

It is declared final, means that it is constant.

String Class is final too. So, StringBuffer and StringBuilder are not subclasses of String Class..!

The array of characters is private. So, internally String is an array, but we don’t get to see it. This…! This is exactly what Encapsulation is..!! Hiding the implementation…! 😉

So, that’s Java’s little back game, it is operating on an array..! So, when we call length() method, this is what it does –

Length in String Class of Java

And all the methods operate on this array and return the results if any or throw exceptions accordingly. One last example to make things clear. This is the implementation of the charAt() method –

CharAt of String Class in Java

When it comes to the StringBuffer and StringBuilder Classes, they both inherit from java.lang.AbstractStringBuilder Class..! There, in the abstract class, the character array is defined, which these classes inherit. This is the definition of the AbstractStringBuilder Class –

AbstractStringBuilder Class in Java

As you can see –

The class is defined as abstract.

It has a member, value, which is an array of characters.

The array is not final.

Instead of relying on value.length, it has a variable count, which stores the characters used

Why is using another variable..? Well… These strings have the notion of capacities and actual sizes. One last thing..! StringBuilder and StringBuffer are final classes, just like many other classes in Java Library.

Summary

Strings in Java are objects of String Class.

Strings created using String Class are immutable. They cannot be modified.

We create mutable strings using StringBuffer and StringBuilder classes. The syntax is –