Concatenating Strings

Another way you can manipulate strings is to concatenate (in other words, join) them. So, when you concatenate two strings, you get a new string that contains both of the original strings. Look at these lines of Java source code, for example. There are several ways to concatenate Strings together. The first is the concat method on the String class.

String str1 = "This is one string";
String str2 = str1.concat(" and this is another.");

If this code fragment was executed, str2 would contain This is one string and this is another and str1 would remain unchanged. As you can see, the concat method's single argument is the string to concatenate with the original string.

To make things simpler, the String class defines an operator, ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.