Java String Concat: Append and Combine Strings

These Java examples use the concat method to combine and append strings. They use the plus sign can concat strings.
Concat. Two strings can be combined with concat. A new string, one with the new data appended, is returned. This can cause performance problems.
With concat, and the plus operator, we use two strings to create a third one. When many appends occur, StringBuilder is often a better, faster choice.
A program. This program uses concat. It takes the initial string "ABC" and concats "DEF." Concat adds to the end of the string—it appends.

Result: The string, named "result," contains the character data from the two string, combined into one string.

Java program that uses concat, String public class Program { public static void main(String[] args) { String value = "ABC"; // Concat another string. String result = value.concat("DEF"); // Display the result. System.out.println(result); } } Output ABCDEF
Concat, chain. The result of concat() is a new String. We can call concat again on this string reference. The resulting, final string, is created by the evaluation of all chained methods.

Warning: Using concat excessively can cause poor performance. Often by coalescing concats we can get better speed.

And: Using StringBuilder to combine strings together, as in a loop, is a still better choice in many programs.

For
Java program that uses concat chained method calls public class Program { public static void main(String[] args) { String value = "123"; // Call concat twice in one statement. String result = value.concat("456").concat("789"); System.out.println(result); } } Output 123456789
Plus sign. Concat can be called through the plus sign. This operator invokes the concat() method. Less typing is required. The syntax may be clearer to read for some developers.
Java program that uses concat, plus sign public class Program { public static void main(String[] args) { String value1 = "abra"; String plus = "ca"; String value2 = "dabra"; // The strings can be concatenated with a plus sign. String result = value1 + plus + value2; System.out.println(result); } } Output abracadabra
Plus, append. With the "+=" operator we can append a string. This is the same as concat, but the result of concat is assigned to the variable. We can use many appends in order.
Java program that appends strings with plus operator public class Program { public static void main(String[] args) { // Append two strings to the initial value. String value = "cat"; value += "10"; value += "10"; // Append a String variable. String animal = "dog"; value += animal; System.out.println(value); } } Output cat1010dog
StringBuilder. We mentioned StringBuilder as a possible optimization for concat. This is effective when we concat in nontrivial loops.

Note: With StringBuilder, we avoid creating a string after each change. Only a mutable buffer is changed.

So: StringBuilder is an effective optimization for complex things, but for few appends it is no better—it may even be slower.

StringBuilder
A summary. With concat or the plus sign, we concatenate (append) strings. This is powerful, but it may be slow in complex operations. StringBuilder is a worthwhile alternative.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls