concatenate two strings java code example

Example 1: String concatenation in java

// String concatenation in java using concat() method
public class StringConcatMethodDemo
{
   public static void main(String[] args)
   {
      String str1 = "Flower ";
      String str2 = "Brackets";
      String str3 = str1.concat(str2);
      System.out.println(str3);
   }
}

Example 2: java concatenate strings

public class Concat {
    String cat(String a, String b) {
        a += b;
        return a;
    }
}

Example 3: how to add strings together

String firstName = "BarackObama";
String lastName = " Care";
//First way
System.out.println(firstName + lastName);
//Second way
String name = firstName + lastName;
System.out.println(name);
//Third way
System.out.println("BarackObama" + " Care");