String Concatenation in JavaIn Java, String concatenation forms a new String that is the combination of multiple strings. There are two ways to concatenate strings in Java:
1) String Concatenation by + (String concatenation) operatorJava String concatenation operator (+) is used to add strings. For Example: TestStringConcatenation1.java Test it NowOutput: Sachin Tendulkar The Java compiler transforms above code to this: In Java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and it's append method. String concatenation operator produces a new String by appending the second operand onto the end of the first operand. The String concatenation operator can concatenate not only String but primitive values also. For Example: TestStringConcatenation2.java Test it NowOutput: 80Sachin4040 Note: After a string literal, all the + will be treated as string concatenation operator.2) String Concatenation by concat() methodThe String concat() method concatenates the specified string to the end of current string. Syntax: Let's see the example of String concat() method. TestStringConcatenation3.java Test it NowOutput: Sachin Tendulkar The above Java program, concatenates two String objects s1 and s2 using concat() method and stores the result into s3 object. There are some other possible ways to concatenate Strings in Java, 1. String concatenation using StringBuilder classStringBuilder is class provides append() method to perform concatenation operation. The append() method accepts arguments of different types like Objects, StringBuilder, int, char, CharSequence, boolean, float, double. StringBuilder is the most popular and fastet way to concatenate strings in Java. It is mutable class which means values stored in StringBuilder objects can be updated or changed. StrBuilder.java Output: 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. 2. String concatenation using format() methodString.format() method allows to concatenate multiple strings using format specifier like %s followed by the string values or objects. StrFormat.java 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. 3. String concatenation using String.join() method (Java Version 8+)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 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. 4. String concatenation using StringJoiner class (Java Version 8+)StringJoiner class has all the functionalities of String.join() method. In advance its constructor can also accept optional arguments, prefix and suffix. StrJoiner.java 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. 5. String concatenation using Collectors.joining() method (Java (Java Version 8+)The Collectors class in Java 8 offers joining() method that concatenates the input elements in a similar order as they occur. ColJoining.java Output: abc, pqr, xyz Here, a list of String array is declared. And a String object str stores the result of Collectors.joining() method. Next TopicSubstring in java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India