String Append Java

Most String manipulation problems requires us to come up with combination of information and summarize into one String. For example, imagine we wish to convert an integer value into words as String, this will require us to combine multiple Strings to come up with an answer String. What is common to this is we need to append one String to another, to form a closer form to the answer. Below we explore how to do String Append in Java.

Java String Append Using Plus Operator

The most common way in Java on how to Append String, is by using the plus operator (+). Plus is natively supported which means combine the two String to the left and right of the plus sign, to form a new String of combined contents. Here is a simple example:

String firstName = "John";
String lastName = "Doe";
String completeName = firstName + lastName;
System.out.println(completeName);

This will comebine the firstName which is John, with the lastName which is Doe, because plus is in the middle of the two variables. Hence, this code will output:

JohnDoe

We can use literal Strings and not only variable. Below is a simple example on how to use literal String to do String Append in Java.
String firstName = "Jane";
String completeName = firstName + "Doe";
System.out.println(completeName);
This appends the contents of the variable firstName, which contains the value Jane, with the literal String Doe. The result is JaneDoe which is the expected output below"
JaneDoe

We can also use the plus operator multiple times in a statement. Below is an example using three items appended together:

String firstName = "John";
String lastName = "Doe";
String completeName = firstName + " - " + lastName;
System.out.println(completeName);

The code above combines the contents of the variable firstName, which have the alue John, with the literal dash and spaces. The result is then again appended to the contents of variable lastName, which contains the value Doe. The resulting String should be "John - Doe", hence the output is shown below:
John - Doe

Java String Append Using StringBuilder

Back in the older versions of Java, using plus operator is inefficient because multiple Strings are created on the fly. Developers typically use StringBuilder as a more efficient method of appending String in Java. Because using StringBuilder is guaranteed not to create unnecessary instances of String. We can feel this when we write a program that handles hundreds if thousands of operations, hence this is very important point to note. In more recent versions of Java, using plus operator uses StringBuilder behind the scenes, and it is not as inefficient as we once believed. Below is a simple example on how to use StringBuilder for Java String Append operations.
String firstName = "John";
String lastName = "Doe";
StringBuilder sb = new StringBuilder();
sb.append(firstName);
sb.append(lastName);
String completeName = sb.toString();
System.out.println(completeName);

As you could see, the above code is longer compared to when we use the plus operator in appending together multiple Strings. But the result should be the same as shown below:

JohnDoe

Initially, the StringBuilder is empty, then the value of firstName was appended. And then the value of the lastName was appended next. Hence the same result is achieved. Below is another example with String literal used in StringBuilder to append Strings together in Java.
String firstName = "Jane";
StringBuilder sb = new StringBuilder();
sb.append(firstName);
sb.append("Doe");
String completeName = sb.toString();
System.out.println(completeName);
We can append together String variables and String literals. Below is the expected output of the above code with StringBuilder.
JaneDoe

Below is another example where we appended together more items.

String firstName = "John";
String lastName = "Doe";
StringBuilder sb = new StringBuilder();
sb.append(firstName);
sb.append(" - ");
sb.append(lastName);
String completeName = sb.toString();
System.out.println(completeName);

As expected, below is the expected output:

John - Doe

Java String Append Using StringBuffer

Why is there StringBuilder and StringBuffer? They feel very similar when used. The difference is that StringBuffer is thread safe and synchronized. But StringBuilder is not thread safe and not synchronized. So why is this important? Why not use StringBuffer always for safety? Because synchronization of StringBuffer comes with a price - it is much slower to use than StringBuilder. StringBuilder is very efficient and fast because it doesn't need to do a lot to ensure thread safety. The performance difference is huge. So what do we do? If we are sure that no threads will share the data structure, use StringBuilder for performance. But if thread safety is important, consider using StringBuffer. The usage is very similar, and we show below a converted example from an example above:
String firstName = "John";
String lastName = "Doe";
StringBuffer sb = new StringBuffer();
sb.append(firstName);
sb.append(" - ");
sb.append(lastName);
String completeName = sb.toString();
System.out.println(completeName);

As expected, below is the expected output:

John - Doe