Week 68 — Which mechanisms does Java provide for concatenating `String`s with other variables?

Question of the Week #68
Which mechanisms does Java provide for concatenating Strings with other variables?
11 Replies
Eric McIntyre
Eric McIntyre8mo ago
String concatenation is the mechanism of combining a String with another variable in order to get another String containing both the original String and the other variable. Java allows doing that using the + operator:
String s = "Hello ";
String t = "World ";
int i = 1337;
String result = s + t + i;
System.out.println(result);//Hello World 1337
String s = "Hello ";
String t = "World ";
int i = 1337;
String result = s + t + i;
System.out.println(result);//Hello World 1337
Other than that, the String class provides a concat method that can concatenate two Strings:
String s = "Hello ";
String t = "World";
String result = s.concat(t);
System.out.println(result);//Hello World
String s = "Hello ";
String t = "World";
String result = s.concat(t);
System.out.println(result);//Hello World
It is also possible to use String.format which allows printf-like format specifiers:
String s = "Hello";
String t = "World";
int i = 1337;
String result = String.format("%s %s %d", s, t, i);
System.out.println(result);//Hello World 1337
String s = "Hello";
String t = "World";
int i = 1337;
String result = String.format("%s %s %d", s, t, i);
System.out.println(result);//Hello World 1337
Aside from that, String templates, which are currently (as of Java 22) a Preview feature, allow doing that as well:
String s = "Hello";
String t = "World";
int i = 1337;
String result = STR."\{s} \{t} \{i}";
System.out.println(result);//Hello World 1337
String s = "Hello";
String t = "World";
int i = 1337;
String result = STR."\{s} \{t} \{i}";
System.out.println(result);//Hello World 1337
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre8mo ago
Java provides + operator to concatenate the string with other variables Like int a = 3; String str = "times"; System.out.print(a+str); // 3times We can also use stringbuilder for optimised concatenation as strings are immutable in java for larger string we use stringbuilder
Submission from rahulsaini_1957
Eric McIntyre
Eric McIntyre8mo ago
Usually you can concatenate with
String + var1
String + var1
Submission from ogbudsz
Eric McIntyre
Eric McIntyre8mo ago
For concatenation of string with other variables addition "+" mechanism is provided by java
Submission from dead_pool4
Eric McIntyre
Eric McIntyre8mo ago
The use of the ‘+’ operator allows the concatenation of various data types to string types. For example str = “Hello” and x = 1 str + x = Hello1 Using the StringBuilder class and using .append() has the same effect though StringBuilder has more methods to use
Submission from _draws
Eric McIntyre
Eric McIntyre8mo ago
For example StringBuilder Or method .concat for string object In the worst case "s += i" or s = "s + i"
Eric McIntyre
Eric McIntyre8mo ago
I'll give code example later if needed
Submission from matieuu
Eric McIntyre
Eric McIntyre8mo ago
In java we can use an empty string Eg: class hi{ void hello{ String a=“hi” String B=“world” String c=a+B}}
Eric McIntyre
Eric McIntyre8mo ago
@JavaBot
Submission from juanisrockingx
Eric McIntyre
Eric McIntyre8mo ago
1. We can use '+' operator.
String name = "Cutie";
int age = 25;
String message = "Hello, my name is " + name + " and I am " + age + " years old.";
System.out.println(message);
String name = "Cutie";
int age = 25;
String message = "Hello, my name is " + name + " and I am " + age + " years old.";
System.out.println(message);
2. Also String.format() is available.
String name = "Cutie";
int age = 25;
String message = "Hello, my name is %s, and I am %d years old.";
System.out.println(message);
String name = "Cutie";
int age = 25;
String message = "Hello, my name is %s, and I am %d years old.";
System.out.println(message);
3. StringBuilder is another option.
String name = "Cutie";
int age = 25;
StringBuilder builder = new StringBuilder();
builder.append("Hello, my name is ").append(name).append(" and I am ").append(age).append(" years old.");
String message = builder.toString();
System.out.println(message);
String name = "Cutie";
int age = 25;
StringBuilder builder = new StringBuilder();
builder.append("Hello, my name is ").append(name).append(" and I am ").append(age).append(" years old.");
String message = builder.toString();
System.out.println(message);
⭐ Submission from cutie.joy
Eric McIntyre
Eric McIntyre8mo ago
** Possible answer: 1) + operator is used to concatenate two strings. 2) StringBuilder also gives us the capability to concatenate strings and other variables. ** 1) Code: String s1 = "java "; String s2 = "week"; System.out.println(s1 + s2); Output: -> java week * 2) Code String str = "java week "; int a = 1; StringBuilder stb = new StringBuilder(); stb.append(str); stb.append(a); System.out.println(stb.toString()); Output: -> java week 1
⭐ Submission from sk001
Want results from more Discord servers?
Add your server