Java: String concatenation

In this article, we will discuss various ways to concatenate strings in Java

 

Various ways to concatenate strings

  • Using concatenation operator (+)
  • Using concat() method of String class

 

There are other ways too, like

  • Using append() method of StringBuffer class
  • Using append() method of StringBuilder class

 

We will discuss string concatenation using “+” operator and concat() method of String class only here

Later separately we will cover string concatenation using append() method of StringBuffer & StringBuilder classes

 

Let’s us explore both ways, one-by-one with example and explanation

1. String concatenation using + operator

Two simple strings can be concatenated using + operator

Example 1: StringConcatenationUsingPlusOperator.java

Say for example, “Bench” and “Resources” are 2 simple strings then using concatenation operator (+) –> both strings can be concatenated to one string as shown in the below example

package in.bench.resources.string.handling.concat;

public class StringConcatenationUsingPlusOperator {

	public static void main(String[] args) {

		String str1 = "Bench";
		String str2 = "Resources";

		// string concatenation using + operator
		String strConcat = str1 + str2;

		// finally printing in the console
		System.out.println(strConcat);
	}
}

Output:

BenchResources

Download: StringConcatenationUsingPlusOperator.java

 

Example 2: ConvertIntegerToStringUsingPlusOperator.java

Integer values can be converted to string using + operator just by adding (“”)

package in.bench.resources.string.handling.concat;

public class ConvertIntegerToStringUsingPlusOperator {

	public static void main(String[] args) {

		String str1 = "Test" + 10 + 20;
		System.out.println(str1);
	}
}

 

Output:

Test1020

Explanation:

  • Though, 10 + 20 should produce 30
  • But due to the presence of string literal “Test”, 10 + 20 will also be treated as string literal instead of simple integer addition
  • Finally, output will be 1020 instead of integer addition 10 + 20 = 30
  • Note: But any integer addition encountered earlier to string literals will be added
  • Lets us see the same in the below example

Download: ConvertIntegerToStringUsingPlusOperator.java

 

Example 3: CombinationOfIntegerAndStringUsingPlusOperator.java

In concatenation of String and Integer using concatenation (+) operator,

  • any integer encountered before any string literal (“”) will be summed up
  • instead of simple string concatenation
  • and following integer values will be concatenated
package in.bench.resources.string.handling.concat;

public class CombinationOfIntegerAndStringUsingPlusOperator {

	public static void main(String[] args) {

		String str1 = 10 + 20 + "Test" + 10 + 20;
		System.out.println(str1);
	}
}

Output:

30Test1020

Explanation:

  • Now, there are 2 integer addition before and after a string literal “Test
  • As we discussed in the earlier example, any integer addition encountered after string literal will also be treated as string literal
  • So, it just gets concatenated (1020)
  • But integer addition before string literal is summed up
  • As in the above example, 10 + 20 will produce 30 and it can be seen in the output

Download: CombinationOfIntegerAndStringUsingPlusOperator.java

 

Points to remember about concatenation operator (+):

  • 2 simple strings can be concatenated using (+) operator
  • It’s not just 2 strings, concatenation operator can be used to concatenate multiple strings
  • Integer values can be converted to string using concatenation (+) operator
  • When there are combination of integer and string, then all preceding integers will be summed up before encountering any string literal (“”)

 

 

2. String concatenation using concat() method of String class

String class’s concat() method concatenates the specified string at the end of current string, just like we have seen examples above using concatenation (+) operator

Multiple concatenation: We can call concat() method multiple times to concatenate multiple strings

StringConcatenationUsingStringConcatMethod.java

package in.bench.resources.string.handling.concat;

public class StringConcatenationUsingStringConcatMethod {

	public static void main(String[] args) {

		String str1 = "Bench";
		String str2 = "Resources";
		String str3 = ".Net";

		// Case 1: simple 2 string concatenation using concat() method
		String twoStr = str1.concat(str2);
		System.out.println(twoStr);

		// Case 2: 3 strings concatenation using concat() method
		String threeStr = str1.concat(str2).concat(str3);
		System.out.println(threeStr);
	}
}

Output:

BenchResources
BenchResources.Net

Download: StringConcatenationUsingStringConcatMethod.java

 

References:

https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!