Concatenating strings

Edit on Github instead

Java uses + to concatenate, or add, strings.

String favorite = "Art History"
favorite = favorite + " is fun!";
favorite                                // "Art History is fun!"

As strings are immutable, concatenation re-assigns the variable's reference rather than changing its value.

String concatenation works so long as all the item's you're assigning can be turned into strings:

String sentence = "My favorite number is ";
int favNum = 3;
String sentence = sentence + favNum;
sentence;                                      // "My favorite number is 3"

Suggest changes

Related links

URL Topic Source