Java String Concatenation
String Concatenation
The +
operator can be used between strings to
combine them. This is called concatenation:
ExampleGet your own Java Server
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
Note that we have added an empty text (" ") to create a space between firstName and lastName on print.
You can also use the concat()
method to concatenate two strings:
Example
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
Exercise?What is this?
Test your skills by answering a few questions about the topics of this page
Which operator can be used to combine strings?