Java Example – String concatenation (join strings)




Java Tutorial for Beginners
Java Tutorial for Beginners

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.

  1. 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.
  2. Using String.concat() method – oncatenates the specified string to the end of this string.
  3. 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
*/



 
 





Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*