«Back to Home

Core Java

Topics

Concatenation String In Java

Concatenation String

String concatenation simply forms a new string, which is the combination of multiple strings.
 
In Java two ways to concat string are given below.
  1. String concatenation by (+) operator.

  2. String concatenation by concat() method.
String Concatenation by (+) operator

With the use of (+) operator, we can add the two strings in Java, which are,
 
Let’s see an example, given below.
 
Code
  1. public class ConcatString {  
  2.     public static void main(String args[]) {  
  3.         String name = "Mia" + " Isabella";  
  4.         String address = "New" + "york";  
  5.         System.out.println(name);  
  6.         System.out.println(address);  
  7.     }  
  8. }  
24
 
Output

25

String Concatenation by concat() method

The string concat() method is used to concatenate the particular string to the end of the current string in Java.
 
Syntax

public String concat(String another)
 
Let's see an example, given below.
 
Code
  1. public class ConcatString {  
  2.     public static void main(String args[]) {  
  3.         String n1 = "Mia ";  
  4.         String n2 = "Isabella";  
  5.         String a1 = "Newyork ";  
  6.         String a2 = "City";  
  7.         String n3 = n1.concat(n2);  
  8.         System.out.println(n3);  
  9.         String a3 = a1.concat(a2);  
  10.         System.out.println(a3);  
  11.     }  
  12. }  
26

Output

27
 
Summary

Thus, we learnt that the string concatenation means to form a new string, which is the combination of the multiple strings and also learnt their ways to concat the string in Java.