JAVA STRING

Java String concatenation

Concatenation joins two or more strings into one. In Java, you can join text using the operator +, the method concat() or dedicated helper classes.

Joining text with the + operator

The easiest starting point is +. Java takes values from left to right and joins them into a new string.

String vardas = "Ieva";
String pasisveikinimas = "Labas, " + vardas + "!";

System.out.println(pasisveikinimas);
Console output

Labas, Ieva!

  1. The variable vardas stores the text Ieva.
  2. The operator + joins the greeting, name and exclamation mark.
  3. The result is stored in a new variable, pasisveikinimas.

Add spaces yourself

Java does not automatically insert spaces between joined words. Write a space as a separate string: " ".

String vardas = "Mantas";
String pavarde = "Kazlauskas";

String pilnasVardas = vardas + " " + pavarde;
System.out.println(pilnasVardas);
Console output

Mantas Kazlauskas

Without the middle " " , the console would show MantasKazlauskas.

Joining text and numbers

When at least one operand of the + operation is a String, Java converts the numbers to text.

String preke = "Obuoliai";
int kiekis = 3;
double kaina = 1.25;

System.out.println(preke + ": " + kiekis + " kg");
System.out.println("Kaina: " + kaina + " Eur");
Console output

Obuoliai: 3 kg
Kaina: 1.25 Eur

Numeric variables remain numbers. They are converted to text only when building the final output string.

Parentheses change the operation order

int pirmas = 4;
int antras = 6;

System.out.println("Suma: " + pirmas + antras);
System.out.println("Suma: " + (pirmas + antras));
Console output

Suma: 46
Suma: 10

  1. In the first line, Java has already encountered the text "Suma: ", so 4 and 6 as characters.
  2. In the second line, parentheses instruct Java to first calculate 4 + 6.
  3. Only the resulting number, 10 , is appended to the text.

The concat() method

concat() appends another String value to the end of a string and returns a new string.

String pradzia = "Geros";
String pabaiga = " dienos!";

String sakinys = pradzia.concat(pabaiga);
System.out.println(sakinys);
Console output

Geros dienos!

Unlike +, the method concat() accepts only a string value.

String values are immutable

Concatenation does not change the old String object. It creates and returns a new result that must be saved.

String tekstas = "Labas";

tekstas.concat(", Rasa");
System.out.println(tekstas);

tekstas = tekstas.concat(", Rasa");
System.out.println(tekstas);
Console output

Labas
Labas, Rasa

  1. The first concat() creates a result but does not save it anywhere.
  2. The first println() therefore still prints only Labas.
  3. The second result is assigned to the variable tekstas, so the full greeting appears later.

Many text changes with StringBuilder

When repeatedly appending text, especially in a loop, it is more convenient to use and its method append().

String[] pirkiniai = {"duona", "pienas", "obuoliai"};
StringBuilder sarasas = new StringBuilder("Pirkiniai: ");

for (int i = 0; i < pirkiniai.length; i++) {
    if (i > 0) {
        sarasas.append(", ");
    }
    sarasas.append(pirkiniai[i]);
}

System.out.println(sarasas);
Console output

Pirkiniai: duona, pienas, obuoliai

StringBuilder extends the same text buffer, making it suitable for many consecutive joins.

Joining values with String.join()

If every value needs the same separator, use String.join().

String dienos = String.join(", ",
        "pirmadienis", "antradienis", "trečiadienis");

System.out.println(dienos);
Console output

pirmadienis, antradienis, trečiadienis

The first argument, ", " specifies what Java inserts between each value.

Which approach should you choose?

Comparing Java text-joining approaches
ApproachWhen is it useful?
+A few values and readable everyday code
concat()When deliberately joining only String value,
StringBuilderWhen text is appended repeatedly or in a loop
String.join()When values share a space, comma or other separator