Java String concatenate example

String concatenate is a way to join multiple Strings to create large String. There are many places where we use to do String concatenate.
For example: When you override toString() method of object, you concatenate multiple attributes to create a big String that represent that object.

3 ways to concatenate String

  1. Using + operator
  2. Using String’s concat method
  3. Using StringBuffer or StringBuilder

+ Operator:

Although java does not support operator overloading but you can concatenate two String using + operator.

Here result will be “HelloJava2blog” .

String’s concat method:

You can use String ‘s concat method to concatenate two String.

Here result will be “HelloJava2blog” .

StringBuilder or StringBuffer:

You can use StringBuilder or StringBuffer’s append method to concatenate two String.

It is fastest way to concatenate two String.

Performance Comparison:

  1. + Operator is not recommended for large String concatenation as it creates lots of temporary object and also some what slow.
  2. If you have lot of concatenation, StringBuffer or StringBuilder is recommended as it is fastest of all.

Java program for String concatenation:

When you run above program, you will get below output

Was this post helpful?

Related Posts

  • 27 September

    Repeat String N times in Java

    Table of ContentsIntroductionWhat is a String in Java?Repeat String N times in JavaSimple For Loop to Repeat String N Times in JavaUsing Recursion to Repeat String N Times in JavaString.format() method to Repeat String N Times in JavaUsing String.repeat() method in Java 11 to Repeat String N TimesRegular Expression – Regex to Repeat String N […]

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-63358d6d4834f427876299/] [crayon-63358d6d48354778443539/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

  • Find character in String in java
    02 February

    Find Character in String in Java

    Table of ContentsHow to Find Character in String in Java1. Using indexOf() Method to Find Character in String in Java2. Using lastIndexOf() Method to Find Char in String in JavaFind Character at Index in String in Javahow Do I Find Word in String in Java? In this post, we will see how to find character […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.