Java String concat() Method with Examples

String concatenation is an essential operation in laptop programming and refers to the method of combining or becoming a member of or more strings together to create a brand new string. This operation permits you to build longer strings by appending one or more strings to an existing one.

In lots of programming languages, along with Java, Python, C++, and many others, there are particular techniques or operators for acting string concatenation.

String Concatenation in Java

In Java, String concatenation paperwork is a new String that is the mixture of a couple of strings.

There are two approaches to concatenating strings in Java:

By + (String concatenation) operator
By concat() method

Difference between concat() and + operator:

Strings are interpreted as character arrays. The difference between a string and a string is that the string ends with the special character “\0”. Since arrays are immutable (can’t grow), strings are also immutable. When a change is made to a string, a new string is created. Connectivity is the process of establishing an end-to-end connection.

Example:

  class TestStringConcatenation1{  
 public static void main(String args[]){  
 String s="Tech"+" Vidvan";  
   System.out.println(s);//TechVidvan 
 }  
}

Output:
TechVidvan

Java String Concatenation by concat() method

The String concat() technique concatenates the specified string to the end of current string.

Syntax:

public String concat(String another)

Example:

  class TestStringConcatenation3{  
 public static void main(String args[]){  
   String s1="Tech";  
  String s2="Vidvan";  
  String s3=s1.concat(s2);  
   System.out.println(s3);//TechVidvan
  }  
}

Output:
TechVidvan

The above Java program concatenates two String objects, s1 and s2, using the concat() method and stores the result in the s3 object.

String concatenation using StringBuilder class

StringBuilder is a class that provides an Append() method to perform concatenation operations. The Append() method accepts parameters of various types such as Objects, StringBuilder, int, char, CharSequence, boolean, float, and double. StringBuilder is the most popular and fastest way to concatenate strings in Java. It is a mutable class, meaning that the values ​​stored in the StringBuilder object can be modified or modified.

Example:

open   course  StrBuilder   {    /* Driver Code */     open   inactive  void main(String args[])    {    StringBuilder s1 =  modern  StringBuilder("Tech"); //String 1    StringBuilder s2 =  unused  StringBuilder("Vidvan"); //String 2    StringBuilder s = s1.append(s2); //String 3 to store the result    System.out.println(s.toString()); // Shows  result    }   }

Output:
TechVidvan

In the above code snippet, s1, s2 and s are declared as objects of StringBuilder class. s stores the result of concatenation of s1 and s2 using append() method.

String concatenation using format() method

String.format() strategy permits to concatenate numerous strings utilizing arrange specifier like %s taken after by the string values or objects.

Example:

     public class StrFormat  
{  
  /* Driver Code */  
    public static void main(String args[])  
   {  
        String s1 = new String("Tech");    //String 1  
       String s2 = new String(" Vidvan");    //String 2  
        String s = String.format("%s%s",s1,s2);   //String 3 to store the result  
            System.out.println(s.toString());  //Displays result  
    }  
}

Output:
TechVidvan

String.format() strategy permits to concatenate numerous strings utilizing arrange specifier like %s take Right here, the String contraptions are relegated the concatenated conclusion result of Strings s1 and s2 the utilization of String.Format() approach. Organize () acknowledges parameters as arranged specifiers taken after through String things or values.

String concatenation using String.join() method.

The String.Join() approach is to be had in Java model 8 and all the above variations. String.Join() technique accepts arguments first, a separator and an array of String items.

Example:

    public class StrJoin  
{  
    /* Driver Code */  
    public static void main(String args[])  
    {  
        String s1 = new String("Tech");    //String 1  
        String s2 = new String("Vidvan");    //String 2  
        String s = String.join("",s1,s2);   //String 3 to store the result  
            System.out.println(s.toString());  //Displays result  
    }  
}

Output:
TechVidvan

In the above code snippet, the String object s stores the result of String.join(“”,s1,s2) method. A separator is specified inside quotation marks followed by the String objects or array of String object.

String concatenation using StringJoiner class

StringJoiner magnificence has all the functionalities of String.Join() approach. Earlier, its constructor also can receive optional arguments, prefix and suffix.

Example:

public class StrJoiner  
{  
    /* Driver Code */  
    public static void main(String args[])  
    {  
        StringJoiner s = new StringJoiner(", ");   //StringJoiner object  
       s.add("Tech");    //String 1   
        s.add("Vidvan");    //String 2  
        System.out.println(s.toString());  //Displays result  
    }  
}

Output:
Tech,Vidvan

In the above script, a StringJoiner object is represented, and the StringJoiner() constructor accepts a delimiter value. Delimiters are special inside quotes. The add() method adds the string beyond the parameter.

Conclusion

The concat() method performs higher than the “+” operator. The latter usually creates a new string, no matter the length of the string. Additionally, we need to take into account that the concat() method simplest creates a new string whilst the string to be appended has a period more than zero.