YouTip LogoYouTip

Java String Concat

# Java concat() Method [![Image 3: Java String Class](#)Java String Class](#) * * * In Java, the `concat()` method is used to concatenate two strings. This method belongs to the `String` class and can be used to append one string to the end of another. It is important to note that the `concat()` method does not modify the original string; instead, it returns a new string. ### Syntax public String concat(String s) ### Parameters * **s** -- The string to be concatenated. ### Return Value Returns the new concatenated string. ### Example ## Example public class Test { public static void main(String args[]){ String s ="Tutorial: "; s = s.concat("www."); System.out.println(s); } } The output of the above program is: Tutorial: www. ### Notes * If the parameter passed to the `concat()` method is `null`, a `NullPointerException` will be thrown. * If the parameter passed to the `concat()` method is an empty string (`""`), the returned string will be the same as the string on which the `concat()` method was called. ## Example public class ConcatExample { public static void main(String[] args){ String str1 ="Hello"; // Concatenate an empty string String result = str1.concat(""); // Print the result System.out.println(result);// Output "Hello" } } In summary, the `concat()` method is a simple and effective way to concatenate strings, but be careful to avoid passing `null` to prevent exceptions. * * Java String Class](#) AI is thinking... [](#)(#) (#)[](#)
← Java String CopyvalueofJava String Compareto β†’