String Concatenation Java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn string concatenation java. String concatenation is one of the paramount method in java.

String Concatenation Java

String concatenation java

There are three methods in java to concatenate strings and they are,

  • StringBuffer.append method
  • StringBuilder class
  • String.concat method
  • Using + operator

Let us understand each method with an example below,

StringBuffer.append method : String concatenation is performed through StringBuffer class which has method called append().

Also Read – Two Best Ways In Java To Sort String Array

Here string concatenation operator produce new string by appending the second operand onto the end of first operand.

Example: string concatenation java

public class JavaStringConcatenation
{
   public static void main(String[] args)
   {
      String strFirst = "Flower";
      String strSecond = "Brackets";
 
      // using StringBuffer.append method
 
      String strConcat = new StringBuffer().append(strFirst).append(strSecond).toString();
 
      System.out.println("String concatenation using StringBuffer.append method - " + strConcat);
   }
}

Output: String concatenation using StringBuffer.append method – FlowerBrackets


String.concat method : String.concat method is one of the method to concatenate two strings.

In this method specified string concatenates to the end of given string.

Let’s see its example below,

Example: string concatenation java

public class JavaStringConcatenation
{
   public static void main(String[] args)
   {
      String strFirst = "Flower";
      String strSecond = "Brackets";
 
      // using String.concat method
 
      String strConcat = strFirst.concat(strSecond);
 
      System.out.println("String concatenation using String.concat method - " + strConcat);
   }
}

Output: String concatenation using String.concat method – FlowerBrackets


StringBuilder class : StringBuilder class is the right way to join multiple strings. Because StringBuilder class represent mutable string.

Concatenating multiple strings do not generate temporary intermediate String object. By doing this we can save lot of memory and reduce garbage collection.

NOTE : Please use StringBuilder class instead of StringBuffer class because it provides the same purpose but without any synchronization overhead, which means faster operation.

public class JavaConcatenateStrings 
{
   public static void main(String[] args) 
   {
      String strOne = "Flower";
      String strTwo = "Brackets"; 
      // concatenating string in java using StringBuilder 
      StringBuilder sb = new StringBuilder(14);
      sb.append(strOne).append(" ").append(strTwo);
      System.out.println("String concatenation in java using StringBuilder : " + sb.toString()); 
   }
}

Output:

String concatenation in java using StringBuilder : Flower  Brackets


Using + operator : In this method of java string concatenation, (+) operator is used which adds string.

Example:

public class JavaStringConcatenation
{
   public static void main(String[] args)
   {
      // using (+) operator to concatenate strings
 
      String strConcat = "Flower" + "Brackets";
 
      System.out.println("String concatenation using (+) operator - " + strConcat);
   }
}

Output: String concatenation using (+) operator – FlowerBrackets


NOTE:

In string concatenation, concatenate operator (+) can concat primitive values also. Let’s take a look at an example,

Example:

public class JavaStringConcatenation
{
   public static void main(String[] args)
   {
      // (+) operator can concat primitive values also
 
      String strConcat = 10 + 4 + "Flower" + "Brackets" + 64 + 37;
 
      System.out.println("(+) operator can concat primitive values also - " + strConcat);
   }
}

Output: 

(+) operator can concat primitive values also – 14FlowerBrackets6437


conclusion

That’s it guys. This was all about how to concatenate strings in java using append string java.

I hope you guys have understood java string concatenation. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like