From the course: Nail Your Java Interview

Concatenate strings with different methods - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Concatenate strings with different methods

- [Instructor] Often in interviews, you'll be asked to manipulate strings in some way, shape or form. The first step to modifying strings, is usually concatenation. The roots of how strings are concatenated or combined, lie in how strings are created. When strings are created they're immutable, you cannot make an already created string longer or shorter. You also can't modify a character that's in an already created string. If you want to do this, you have to create a new string. This means when we're concatenating two strings, we aren't changing their contents, instead, we are reading the values of the two strings and creating a new string leaving the old strings on altered. So how do we concatenate or combine strings? We have a few options. The most common is with the concatenation operator, or the plus sign. In creating the string variable name, the first name and last name variables were not modified. Instead, a new string was created with the first name and the space, and then another new string was created combining the first name space and the last name. This means two strings had to be created in order to make the name string. Again, this happens because strings are immutable once created and cannot be modified. Another option for concatenation, is what the concat string instance method. We can write string concat name, firstName.concat with the space, and then we will concat the last name on top of that. This is a little less readable but it essentially does the same thing. The first name and space are combined into one new string, and then this new string is concatenated with the last name string. With both of these concatenation methods, we create an extra string that we immediately throw out. The first name concatenated with the space. To make this more efficient, we can use the string builder class. Since strings are immutable, classes like string builder allow you to create string like objects that are mutable. Instead of creating a bunch of new strings to complete the concatenation, you simply create a string builder object, run the appropriate mutations with the pend, insert and delete methods, and then call two string to create the final string. Here, we did all of our operations on the one string builder instance, instead of creating a bunch of strings that we weren't going to use. There is also another class called string buffer, and it's pretty similar to the string builder class. The difference, it's thread safe because it's methods are synchronized. When you are asked to manipulate or work with strings in a technical interview, you should use a string builder or string buffer object, in order to make your programs more efficient. Understanding the background behind why these modifier objects are useful, can help you stand out as a candidate.

Contents