How do I concatenate two strings in Java?
To concatenate two strings in Java, you can use the + operator. The + operator is used to add two numbers, but when used with strings, it concatenates the strings.
Here is an example of concatenating two strings using the + operator:
String str1 = "Hello";
String str2 = "World";
String str3 = str1 + " " + str2;
System.out.println(str3); // prints "Hello World"In this example, the + operator is used to concatenate the str1 and str2 strings with a space character in between. The resulting string is stored in the str3 variable.
You can also use the String.concat() method to concatenate two strings:
String str1 = "Hello";
String str2 = "World";
String str3 = str1.concat(" ").concat(str2);
System.out.println(str3); // prints "Hello World"In this example, the concat() method is called on the str1 string to concatenate a space character, and then called on the resulting string to concatenate the str2 string.
I hope this helps. Let me know if you have any questions.