Java String Concatenation

In this chapter you will learn:

  1. How to add strings together
  2. How to concatenate String with other data types
  3. How to concatenate one string to the other with concat method

Add strings together

You can use + operator to concatenate strings together. For example, the following fragment concatenates three strings:

public class Main {
  public static void main(String[] argv) {
    String age = "9";
    String s = "He is " + age + " years old.";
    System.out.println(s);//from   java  2  s .  co m
  }
}

The following code uses string concatenation to create a very long string.

public class Main {
  public static void main(String args[]) {
/*from   j a v a  2s .  c o m*/
    String longStr = "A demo 2s.      com" + 
                     "B d e m o 2 s . c o m                     " + 
                     "C demo                 2s.com" + 
                     "D demo2s.com .";

    System.out.println(longStr);
  }
}

Concatenate String with Other Data Types

You can concatenate strings with other types of data.

public class Main {
  public static void main(String[] argv) {
    int age = 1;//  j a v a  2  s.com
    String s = "He is " + age + " years old.";
    System.out.println(s);
  }
}

The output:

Be careful when you mix other types of operations with string concatenation. Consider the following:

public class Main {
  public static void main(String[] argv) {
    String s = "four: " + 2 + 2;
    System.out.println(s);// j  a  v a 2 s  . c  om
  }
}

This fragment displays

rather than the

To complete the integer addition first, you must use parentheses, like this:

String s = "four: " + (2 + 2);

Now s contains the string "four: 4".

concat one string to the other with concat method

String concat(String str) concatenates the specified string to the end of this string.

public class Main {
  public static void main(String[] argv) {
    String str = "demo2s.com";
    //from   j a  va 2s .  c  om
    str = str.concat("Demo2s.com");
    System.out.println(str);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to create String object with String constructors
  2. How to create a string from byte array
  3. How to create a string from part of a byte array
  4. How to create String from char array
  5. How to create String from part of a char array
  6. How to create a String from another string
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words