How to concatenate multiple strings in Java?
In Java, you can use the plus sign operator (+) to concatenate multiple strings. For example:
String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2;
System.out.println(result); // 输出:Hello World
Another commonly used method is to use the StringBuilder class. The StringBuilder class provides an append method that can be used to concatenate multiple strings. For example:
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
System.out.println(result); // 输出:Hello World
The advantage of using the StringBuilder class is that it offers better performance than using the plus operator when concatenating a large number of strings, as the StringBuilder class internally uses a mutable buffer.