String concatenation in java

Let’s learn string concatenation in java.

String concatenation in java

String concatenation is nothing but combining two or more strings to form a new string.

Let’s learn few ways to concatenate strings in java.

  • String concatenation using concat() method
  • String concatenation using + (string concatenation) operator
  • String concatenation by StringBuilder

String concatenation using concat() method: String concat() method concatenates specified string to end of given string.

Also read – sparse matrix and its representations in java

Let’s see an example of 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);
   }
}


Output:

string concatenation in java

String concatenation using + (string concatenation) operator:  string concatenation operator (+) is used to add or concatenate strings and it considered as best way to concatenate strings in java. Here’s an example.

public class StringConcatenationDemo
{
   public static void main(String[] args)
   {
      String str = "Flower" + " Brackets";
      System.out.println(str);
   }
}


Output:

Flower Brackets


But java compiler transforms above code to,

String str = (new StringBuilder()).append("Flower").append(" Brackets").toString();

Because concatenation is executed using StringBuilder or StringBuffer class through its append() method.


We can use concat() method on primitive values as well. For example

public class StringConcatenationDemo
{
   public static void main(String[] args)
   {
      String str = 40 + 10 + "Arjun" + 60 + 80;
      System.out.println(str);
   }
}


Output:

50Arjun6080


String concatenation using StringBuilder

Unlike String, we can mute StringBuilder class. The primary operation of a StringBuilder are append and insert which are overloaded to accept data of any type.

public static String concat(String str1, String str2)
{
   return new StringBuilder(str1).append(str2).toString();
}


How to concatenate two strings in java using scanner

Now let’s learn how to concatenate two strings in java using scanner.

To concatenate two strings we are using string concatenation operator (+).

Here’s the java program to concatenate two strings using scanner.

import java.util.Scanner;
public class StringConcatenationDemo 
{
   public static void main(String[] args) 
   {
      String str1, str2;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first string: ");
      str1 = sc.nextLine();
      System.out.println("Please enter second string: ");
      str2 = sc.nextLine();
      System.out.println("Concatenate two strings in java using scanner: ");
      System.out.println(str1 + " " + str2);
      sc.close();
   }
}


Output:

Please enter first string:
hello
Please enter second string:
world
Concatenate two strings in java using scanner:
hello world


string concatenation in java without using concat function

Here’s java program to concatenate two strings without using concat function.

import java.util.Scanner;
class WithoutUsingConcatFunction
{
   public static void main(String[] args)
   {
      String one, two;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first string: ");
      one = sc.nextLine();
      System.out.println("Please enter second string: ");
      two = sc.nextLine();
      System.out.println("String without using concat function: ");
      System.out.println(one + two);
      sc.close();
   }
}


Output:

Please enter first string:
flower
Please enter second string:
brackets
String without using concat function:
flowerbrackets