Setting up the Stage:
Let's say you add two numbers in Python and you want to print the output on the console using print() function. To achieve this we can make use of something called as String Interpolation.
num1 = 10.125
num2 = 11.525
sum = num1 + num2
print(f"The sum of {num1:} and {num2} is {sum:.3f}")
Result:
Now if you want to do something similar in other languages that support string interpolation we be like below.
Language | Equivalent String Interpolation |
---|---|
C# | $"The sum of {num1} and {num2} is {num1 + num2:F2}" |
Visual Basic | $"The sum of {num1} and {num2} is {num1 + num2:F2}" |
Python | f"The sum of {num1} and {num2} is {result:.2f}" |
Scala | s"The sum of $num1 and $num2 is ${num1 + num2}" |
Groovy | "The sum of $num1 and $num2 is ${num1 + num2}" |
Kotlin | "The sum of $num1 and $num2 is ${num1 + num2}" |
JavaScript | The sum of ${num1} and ${num2} is ${num1 + num2} |
Ruby | "The sum of #{num1} and #{num2} is #{num1 + num2}" |
Swift | "\(num1) plus \(num2) equals \(num1 + num2)" |
Go (Golang) | fmt.Sprintf("The sum of %v and %v is %.2f", num1, num2, num1+num2) |
Rust | format!("The sum of {} and {:.2} is {:.2}", num1, num2, num1 + num2); |
How we deal with same Program in Java (prior to String Template)
- Using String Concatenation Operator +
- Using MessageFormat
MessageFormat messageFormat = new MessageFormat("The sum of {0} and {1} equals {2}"); String resultString = messageFormat.format(num1, num2, num1 + num2);
- Using StringBuilder
String stringResult = new StringBuilder() .append("The Sum of ") .append(num1) .append(" and ") .append(num2) .append(" is ") .append(num1 + num2) .toString();
- Using String::format
String.format("The sum of %2$d and %1$d is %3$d", num1, num2, num1 + num2);
- Using String::formatted
"The sum of %2$d and %1$d is %3$d".formatted(num1, num2, num1 + num2);
Example:
double result = num1 + num2;
NumberFormatter numberFormatter = new NumberFormatter();
String formattedResult = numberFormatter.format(result);
String result = "The sum of " + num1 + " and " + num2 + " is " + formattedResult;
class NumberFormatter {
public String format(double number) {
return String.format("%.2f", number);
}
}
JEP 430 String Templates
As you saw in the above section, that most of the other programming languages supports string interpolation which is very convenient than concatenation. It makes the code more readable and provides more clarity especially when you have to deal with larger strings.
STR - Template expressions in JDK 21 are a new kind of expression in the Java programming language that can be used to perform string interpolation and can also turn structured text into any kind of object, according to domain-specific rules.
Example:
public class Hello {
public static void main(String... args) {
String name = "Sam";
System.out.println(STR."Hello, my name is \{name}");
}
}
Let's see the how with our sum of two numbers example works with Java String Template example.
public class Hello {
public static void main(String... args) {
double num1 = 10.125;
double num2 = 15.625;
System.out.println(STR."The sum of \{num1} and \{num2} is \{num1+num2}");
}
}
Output: The sum of 10.125 and 15.625 is 25.75