Java String concatenation FAQ: How do I merge/combine two Java String fields?
You can merge / concatenate /combine two Java String fields using the
+ operator, as shown in this Java String example code:
String firstName = "Fred"; String lastName = "Flinstone"; String fullName = firstName + lastName;
fullName now contains "FredFlinstone". There is no space between "Fred" and "Flinstone" because we did not put a space in there.
One way to add a space to our new Java String is to put one in there, as shown in this example Java String merging code:
String firstName = "Fred"; String lastName = "Flinstone"; String fullName = firstName + " " + lastName;
In that example, we are really combining/merging three Java Strings on that last line of code.
I hope these Java String concatenation examples have been helpful. As you can see, for simple string concatenation operations you can just add Java Strings together using the "+" operator. But as an important note, if you're going to be adding a lot of Java String objects together, it's much more efficient to use the Java StringBuffer class. Just search this site for "Java StringBuffer" to find more String concatenation examples.