In the latest JEPs https://openjdk.java.net/jeps/355, we have the only expectation of the RAW string literals, but there is nothing about the string interpolation.
And it’s so sad, that we need writing code like this in the 2020 year:
int a = 3;
int b = 4;
System.out.println(a + " + " + b + " = " + (a + b));
just to print the string: 3 + 4 = 7
of course, we can use a var
since Java 10:
var a = 3;
var b = 4;
System.out.println(a + " + " + b + " = " + (a + b));
But this code is still sad =(
var a = 3;
var b = 4;
System.out.println("${a} + ${b} = ${a+b}");
prints: 3 + 4 = 7
var a = 3;
var b = 4;
System.out.println("flag = ${a > b ? true : false}");
prints: flag = false
var a = 3;
System.out.println("pow = ${a * a}");
prints: pow = 9
You need to add the following dependency:
<dependency>
<groupId>com.antkorwin</groupId>
<artifactId>better-strings</artifactId>
<version>0.1</version>
</dependency>
And you can use string interpolation anywhere in your code.
To skip the string interpolation for class, method or field you can use the @DisabledStringInterpolation
annotation:
@DisabledStringInterpolation
class Foo {
void test() {
System.out.println("${a+b}");
}
}
this code prints: ${a+b}