In this article, we will explain java string concatenation in detail. Also, we will see different ways in which two or more strings can be concatenated in java. We will also see the performance of string concatenation in java. In addition, there will be java code examples showing concatenation of java strings.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Java String concatenation can be defined as the process of joining two or more strings together into a new string. The following are different ways of concatenating a string in java:
This is the simplest way of concatenating java strings. Using the + operator, we can combine more than one string. One important thing to be noted is that the String obtained after concatenation using + operator will be a new String pointing to the fresh memory location in the java heap. If a string object exists in the pool, then it returns the same string object; otherwise, it creates a new object. The following are the most important things to be kept in mind while using the + operator for concatenating strings:
This is a more efficient way to concatenate java strings. Using String buffer for concatenating strings does not create a new object of the resulting string. Using String Buffer saves a lot of memory space as compared to using the + operator. When using string buffer for string concatenation, initialize string buffer object with a specified capacity equal to a number of characters in the final concatenated string. This will result in efficient memory usage as well as save time spent during the resizing of characters. The only drawback associated with using string buffer for string concatenation is the synchronization overhead involved in it as a string buffer is thread-safe.
This is the most efficient way to concatenate java strings. Using String builder for concatenating strings does not create a new object of the resulting string. Using String Builder saves a lot of memory space as compared to using the + operator. When using string builder for string concatenation, initialize string builder object with a specified capacity equal to a number of characters in the final concatenated string. This will result in efficient memory usage as well as save time spent during the resizing of characters. Using a string builder is considered better than using a string buffer because there is no synchronization overhead involved when using a string builder.
This is implemented internally using string buffer or string builder append methods which are described above.
From the four-string concatenation methods described earlier, using the + operator is preferred when fewer strings are to be concatenated. But as the number of strings to be concatenated becomes larger, the performance of the + operator degrades a large number of objects are created due to string immutability. When several strings are to be concatenated, a string builder is preferred as it provides string mutability.
Syntax:
Here is the basic syntax of string concatenation in java:
1. Using + operator:
// method accepting strings to be concatenated public String concatString(String s1, String s2){ String s3= s1+s2; // using + operator return s3; }
2. Using String.concat() method:
public String concatString(String s1){ String s= "First"; String s2=s.concat(s1); // using concat method return s2; }
3. Using a StringBuffer:
StringBuffer sb= new StringBuffer(capacity); // create string buffer instance sb.append("first").append(" ").append("second"); // call append method specifying string to be concatenated
4. Using StringBuilder:
StringBuilder sb= new StringBuilder(capacity); // create string builder instance sb.append("first").append(" ").append("second"); // call append method specifying string to be concatenated
Given below are the examples:
Let us see a basic example of string concatenation using the + operator and concat () method.
Code:
package com.edubca.stringconcatenation; public class StringconcatDemo{ public static void main(String args[]){ String s1= "This"; String s2= "Is"; String s3= "Edubca"; String s4="Training"; // Using + operator String concatenatedstr= s1 + " " + s2 +" " + s3 + " " + s4; System.out.println("String after concatenation is :: " + concatenatedstr); //using concat method String concatenatedstr1= s1.concat(" ").concat(s2).concat(" ").concat(s3).concat(" ").concat(s4); System.out.println("String after concatenation using concat method is :: " + concatenatedstr1); } }
Output:
In this example, we will see how to string concatenation is achieved using a string buffer in java.
Code:
package com.edubca.stringconcatenation; public class StringconcatDemo{ public static void main(String args[]){ String s1= "This"; String s2= "Is"; String s3= "Edubca"; String s4="Training"; // create StringBuffer Instance StringBuffer sb=new StringBuffer("Edubca"); //call append method for string concatenate StringBuffer concatenatedstr=sb.append(" ").append("Java").append(" ").append("Training"); System.out.println("Concatenated String using String Buffer is :: " + concatenatedstr ); } }
Output:
In this example, we will see how to string concatenation is achieved using a string builder in java.
Code:
package com.edubca.stringconcatenation; public class StringconcatDemo{ public static void main(String args[]){ String s1= "This"; String s2= "Is"; String s3= "Edubca"; String s4="Training"; // create StringBuilder Instance StringBuilder sb=new StringBuilder ("Edubca"); //call append method for string concatenate StringBuilder concatenatedstr=sb.append(" ").append("Java").append(" ").append("Training"); System.out.println("Concatenated String using String Builder is :: " + concatenatedstr ); } }
Output:
Note: It is important to note that we should prefer StringBuilder over StringBuffer as a String buffer that consumes extra time in synchronization. But in a multithreaded environment, it is advisable to go for StringBuffer.From the above discussion, we have a clear understanding of string concatenation in java, how it’s achieved, and what are different ways to concatenate java strings. Also, java code examples provide a clear understanding of concatenating strings.
The above is the detailed content of Java String Concatenation. For more information, please follow other related articles on the PHP Chinese website!