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);Labas, Ieva!
- The variable
vardasstores the textIeva. - The operator
+joins the greeting, name and exclamation mark. - 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);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");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));Suma: 46
Suma: 10
- In the first line, Java has already encountered the text
"Suma: ", so4and6as characters. - In the second line, parentheses instruct Java to first calculate
4 + 6. - 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);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);Labas
Labas, Rasa
- The first
concat()creates a result but does not save it anywhere. - The first
println()therefore still prints only Labas. - 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 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);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);pirmadienis, antradienis, trečiadienis
The first argument, ", " specifies what Java inserts between each value.
Which approach should you choose?
| Approach | When is it useful? |
|---|---|
+ | A few values and readable everyday code |
concat() | When deliberately joining only String value, |
StringBuilder | When text is appended repeatedly or in a loop |
String.join() | When values share a space, comma or other separator |