1// String concatenation in java using concat() method
2public class StringConcatMethodDemo
3{
4 public static void main(String[] args)
5 {
6 String str1 = "Flower ";
7 String str2 = "Brackets";
8 String str3 = str1.concat(str2);
9 System.out.println(str3);
10 }
11}
1// StringBuilder
2String stringBuilderConcat = new StringBuilder()
3 .append(greeting)
4 .append(" ")
5 .append(person)
6 .append("! Welcome to the ")
7 .append(location)
8 .append("!")
9 .build();
1public class ConcatenationExample {
2 public static void main(String args[]) {
3 //One way of doing concatenation
4 String str1 = "Welcome";
5 str1 = str1.concat(" to ");
6 str1 = str1.concat(" String handling ");
7 System.out.println(str1);
8
9 //Other way of doing concatenation in one line
10 String str2 = "This";
11 str2 = str2.concat(" is").concat(" just a").concat(" String");
12 System.out.println(str2);
13 }
14}
1public class Concat {
2 String cat(String a, String b) {
3 a += b;
4 return a;
5 }
6}
1// Java program to demonstrate
2// working of concat() method
3
4class Gfg {
5 public static void main(String args[])
6 {
7 String s = "Gfg";
8 s = s.concat("! is the best.");
9 System.out.println(s);
10 }
11}
12
1String s="Sachin"+" Tendulkar";
2 System.out.println(s);//Sachin Tendulkar
3
4Output:Sachin Tendulkar
5