The Java String.concat() concatenates the specified string to the end of the current string.
Internally, Java creates a new character array with a combined length of the current and argument string. Then it copies all content from both strings into this new array. Finally, the combined character array is converted to a new string and returned.
1. String.concat() API
The concat() API concatenates the specified string to the end of this string.
public String concat(String str);
2. String.concat() Example
The following Java program concatenates two strings to produce a combined string.
String str = "Hello";
Assertions.assertEquals("Hello World", str.concat(" World"));
3. Null and Empty Strings
Note that the method will return the original string if we can pass an empty string as the argument string.
Assertions.assertEquals("Hello", str.concat(""));
The null is not an accepted input and causes NullPointerException.
Assertions.assertThrows(NullPointerException.class, ()->{
str.concat(null);
});
Happy Learning !!
References: Java String Doc