ZetCode

Java add string

last modified July 15, 2022

Java add string tutorial shows how to concatenate strings in Java.

In Java, a string is a sequence of Unicode characters. Strings are objects. There are two basic classes for working with strings:

There are several ways how to add strings in C#:

Java add strings with + operator

The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded.

com/zetcode/AddStrings.java
package com.zetcode;

public class AddStrings {

    public static void main(String[] args) {

        System.out.println("Return" + " of " + "the king.");

        String msg = "There are";
        msg += " three";
        msg += " falcons";
        msg += " in the sky";

        System.out.println(msg);
    }
}

The example adds two strings using the + and += operators.

$ java com/zetcode/AddStrings.java
Return of the king.
There are three falcons in the sky

Java add strings with String.concat

The String.concat method concatenates the specified string to the end of this string.

com/zetcode/AddStrings.java
package com.zetcode;

public class AddStrings {

    public static void main(String[] args) {

        System.out.println("Return".concat(" of ").concat("the king."));
    }
}

In the example, we add strings with String.concat.

Java add strings with String.join

The String.join method returns a new atring composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

com/zetcode/AddStrings.java
package com.zetcode;

public class AddStrings {

    public static void main(String[] args) {

        String[] words = { "There", "are", "two", "owls", "on", "the", "tree" };
        String msg = String.join(" ", words);

        System.out.println(msg);
    }
}

We have an array of words. We form a sentence from the words with the String.join method.

$ java com/zetcode/AddStrings.java
There are two owls on the tree

Java add strings with StringBuilder

StringBuilder is a mutable sequence of characters. Its append method appends the specified string to the string instance.

com/zetcode/AddStrings.java
package com.zetcode;

public class AddStrings {

    public static void main(String[] args) {

        var sb = new StringBuilder();
        sb.append("Return");
        sb.append(" of ");
        sb.append("the king.");

        System.out.println(sb);
    }
}

In the example, we form a new string with StringBuilder.

$ java com/zetcode/AddStrings.java
Return of the king.

Java add strings with String.format

The String.format method returns a formatted string using the specified format string and arguments.

com/zetcode/AddStrings.java
package com.zetcode;

public class AddStrings {

    public static void main(String[] args) {

        var w1 = "three";
        var w2 = "owls";
        
        var msg = String.format("There are %s %s on the tree", w1, w2);
        System.out.println(msg);
    }
}

In the example, we build a new string with String.format.

In this article we have showed how to add strings in Java.

List all Java tutorials.