JDK 8 added a new method to String called join(). It concatenate two or more strings separated by a delimiter. In the below example first parameter to the method treated as delimiter.
public class StringJoin { public static void main(String[] args) { String result = String.join(", ", "I ", "am", "a", "Java" , "lover."); System.out.println(result); result = String.join(" ", "I ", "am", "a", "Java" , "lover."); System.out.println(result); }
output:
I , am, a, Java, lover.
I am a Java lover.
By Using concatenate operator (+) symbol , StringBuffer or StringBuilder also we can concatenate two or more Strings.
public class StringJoin { public static void main(String[] args) { String result = "I " + "am" + "a" + "Java" + "lover."; System.out.println(result); result = new StringBuffer("I ").append( "am") .append("a").append("Java").append("lover.").toString(); System.out.println(result); result = new StringBuilder("I ").append( "am") .append("a").append("Java").append("lover.").toString(); System.out.println(result); } }
output:
I amaJavalover.
I amaJavalover.
I amaJavalover.