3.string Concatenation in Java - Javatpoint
3.string Concatenation in Java - Javatpoint
ADVERTISEMENT
2. By concat() method
TestStringConcatenation1.java
class TestStringConcatenation1{
public static void main(String args[]){
String s="Sachin"+" Tendulkar";
System.out.println(s);//Sachin Tendulkar
}
https://www.javatpoint.com/string-concatenation-in-java 2/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
Test it Now
Output:
ADVERTISEMENT
Sachin Tendulkar
https://www.javatpoint.com/string-concatenation-in-java 3/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
String s=
(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();
TestStringConcatenation2.java
class TestStringConcatenation2{
public static void main(String args[]){
String s=50+30+"Sachin"+40+40;
System.out.println(s);//80Sachin4040
}
}
Test it Now
Output:
80Sachin4040
https://www.javatpoint.com/string-concatenation-in-java 4/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
Note: After a string literal, all the + will be treated as string concatenation
operator.
The String concat() method concatenates the specified string to the end of
current string. Syntax:
TestStringConcatenation3.java
class TestStringConcatenation3{
https://www.javatpoint.com/string-concatenation-in-java 5/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
Test it Now
Output:
Sachin Tendulkar
The above Java program, concatenates two String objects s1 and s2 using
concat() method and stores the result into s3 object.
https://www.javatpoint.com/string-concatenation-in-java 6/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
StrBuilder.java
Output:
https://www.javatpoint.com/string-concatenation-in-java 7/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
Hello World
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.
StrFormat.java
{
String s1 = new String("Hello"); //String 1
String s2 = new String(" World"); //String 2
String s = String.format("%s%s",s1,s2); //String 3 to store the result
System.out.println(s.toString()); //Displays result
}
}
Output:
Hello World
Here, the String objects s is assigned the concatenated result of Strings s1 and
s2 using String.format() method. format() accepts parameters as format
specifier followed by String objects or values.
https://www.javatpoint.com/string-concatenation-in-java 9/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
The String.join() method is available in Java version 8 and all the above versions.
String.join() method accepts arguments first a separator and an array of String
objects.
StrJoin.java:
Output:
Hello World
https://www.javatpoint.com/string-concatenation-in-java 10/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
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 objects.
StrJoiner.java
https://www.javatpoint.com/string-concatenation-in-java 11/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
Output:
Hello, World
In the above code snippet, the StringJoiner object s is declared and the
constructor StringJoiner() accepts a separator value. A separator is specified
inside quotation marks. The add() method appends Strings passed as
arguments.
The Collectors class in Java 8 offers joining() method that concatenates the input
elements in a similar order as they occur.
ColJoining.java
import java.util.*;
import java.util.stream.Collectors;
public class ColJoining
https://www.javatpoint.com/string-concatenation-in-java 12/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
{
/* Driver Code */
public static void main(String args[])
{
List<String> liststr = Arrays.asList("abc", "pqr", "xyz"); //List of String array
String str = liststr.stream().collect(Collectors.joining(", ")); //performs joining o
System.out.println(str.toString()); //Displays result
}
}
Output:
Here, a list of String array is declared. And a String object str stores the result of
Collectors.joining() method.
← Prev Next →
https://www.javatpoint.com/string-concatenation-in-java 13/19
3/28/24, 12:11 PM String Concatenation in Java - javatpoint
Feedback
https://www.javatpoint.com/string-concatenation-in-java 14/19