Concatenation is the process of combining two or more small strings to from a bigger string. String concatenation in java can be done in four different ways given below.
1. Using + operator
2. Using concat() method of String class
3. Using append() method of StringBuffer class
4. Using append() method of StringBuilder class
1. Using + operator
2. Using concat() method of String class
3. Using append() method of StringBuffer class
4. Using append() method of StringBuilder class
In below program I have implemented these four methods to concatenate two strings. If you find anything missing or incorrect then please mention it by commenting below. Feel free to ask your queries if you are getting any problem to understand the program.
Program for String Concatenation in Java
class StringConcatenation { public static void main(String...s) { String s1="Hello",s2="World",s3,s4; s3=s1+s2; System.out.println(s3); s4=s1.concat(s2); System.out.println(s4); StringBuffer s5=new StringBuffer().append(s1).append(s2); System.out.println(s5); StringBuilder s6=new StringBuilder().append(s1).append(s2); System.out.println(s6); } }
You’ve captured this pertlcfey. Thanks for taking the time!