String concatenation is the operation of joining character strings end-to-end.
In this Java example we will see How to concatenate Strings in Java.
The String concatenation in Java can be done in several ways.
- Using + operator – When we use + operator
string_1 + string_2
statement, it would be executed as,new StringBuffer().append(string_1 ).append(string_2)
Internally. + operator is not recommended for large number of String concatenation as the performance is not good. - Using
String.concat()
method – oncatenates the specified string to the end of this string. - Using
StringBuffer.append
method – Appends the specified StringBuffer to this sequence.
/* Java String concatenation Example by codebind.com. */ public class JavaStringConcat { public static void main(String args[]){ String string_1 = "Hello"; String string_2 = " World"; //1. String concatenation Using + operator String string_3 = string_1 + string_2; System.out.println("+ operator : " + string_3); //2. String concatenation Using String.concat() method String string_4 = string_1.concat(string_2); System.out.println("String concat method : " + string_4); //3. String concatenation Using StringBuffer.append method String string_5 = new StringBuffer().append(string_1).append(string_2).toString(); System.out.println("StringBuffer append method : " + string_5); } } /* Output : + operator : Hello World String concat method : Hello World StringBuffer append method : Hello World */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
Leave a Reply