Concatenation
Lesson 4 of 16 in Coddy's Strings and Arrays in Java course.
It's possible to combine strings using the + operator,
String str1 = "Hello";
String str2 = "World";
System.out.println(str1 + " " + str2);The string "Hello World" will be printed in the above example.
Note the space (
" ") added in betweenstr1andstr2
Or, use the concat() method,
String str1 = "Hello ";
String str2 = "World";
System.out.println(str1.concat(str2));This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyWrite a function named createFullName that gets two strings as arguments, the first name and the last name, and returns the concatenation of the two strings with a space in between.
Try it yourself
class CreateFullName {
public static String createFullName(String firstName, String lastName) {
// Write code here
}
}