Java – String concatenation with example

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

Various ways to Concatenate Strings :

  1. Using concatenation operator (+)
  2. Using concat() method of String class

There are other ways too, like

  1. Using append() method of StringBuffer class
  2. Using append() method of StringBuilder class

Here, we will discuss string concatenation using “+” operator and String class’ concat() method only

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

  • 2 string can be concatenated using + operator

StringConcatenationUsingPlusOperator.java

  • Say for example, “Bench” and “Resources” are 2 simple strings
  • Then using concatenation (+) operator, both strings can be concatenated to 1 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

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

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

1.1 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

  • 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: 2 string concatenated using concat() method
		String twoStr = str1.concat(str2);
		System.out.println(twoStr);

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

Output:

BenchResources
BenchResources.Net

Download: StringConcatenationUsingStringConcatMethod.java

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String charAt(int index) method
Java - String comparison with example