Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The string class is one of the most common complex classes you'll use in Java. The string class has a special characteristic, it's immutable. This means that when you set a value of a string object that value cannot be changed. Now it might seem like you can change it. Let's take some very simple code, I have declared a string called s1 and given it a value of Welcome, I'll place the cursor on the next line and say s1 = s1+ "to California!"; you could also write this code using the += operator, either way it looks like you're appending to an existing value and simply changing the value of an existing variable.
Then I'll output the value of that variable to the screen using System.out.println, and I'll parse in s1 and I'll Run the application and everything looks like it's working just fine, but it's not so great in the background. What's really happening is that when you append a value or otherwise change the value of the string variable, you're actually creating a new instance of the string class, and you're abandoning the reference to the old object. That old object is still out there in memory; it becomes eligible for garbage collection.
But in fact, you're using more memory than you need to or should. You won't see this behaviorally in your applications, the issue only becomes apparent when you're trying to tune your applications in a small memory environment, or you're working with very large scale applications. But because this is a universal issue in Java, it's good to know how to deal with it from the beginning. Java provides two utility classes called StringBuffer and StringBuilder, these two classes have the same API or programming interface, and they both implement the same methods named insert and append.
The insert method can be used to insert text into a string at either the beginning or any other position in the string. And the append method can be used to append text to a string. You can use either the StringBuilder or the StringBuffer in most environments, but in general, the StringBuilder is more slender, it takes less memory and resources, but it's only good for single threaded environments. StringBuffer should be used where you need to synchronize the use of a string among multiple threads. If you're new to Java don't worry about it, just use StringBuilder until you find out you have to StringBuffer.
So here is an example of how you might use StringBuilder. I'm going to take this little bit of code out and leave myself with the simple s1 variable and then after I've created the initial string, I'll create another variable named StringBuilder, notice that StringBuilder and StringBuffer are both members of the package java.lang, so they're always available to your code. So I'll declare the variable StringBuilder and I'll name sb and I'll instantiate it using new StringBuilder, I'll place the cursor inside the parentheses and press Ctrl+Space and show you the different ways that you can create a StringBuilder.
You can either call the constructor method with no arguments or with some of these other settings, but the one I'm going to use is an initial string and I'll parse in s1. So now my StringBuilder class is being created and it's been populated with that initial value. Now to append a value, I'll call sb. append and I'll parse in that appended value that I used before, "to California!" and I'll change my print line command to output sb, the StringBuilder. I'll Run the application and I get the same result.
Whenever you're working with strings that you need to manipulate, either adding text or inserting text, use the StringBuilder class instead of a simple string, you'll create fewer objects and you'll conserve memory.
Get unlimited access to all courses for just $25/month.Become a member