JDK-8267230 : Implement ES6-style template literals in Java
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2021-05-16
  • Updated: 2023-09-06
  • Resolved: 2023-09-06
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
From what I can tell, this has not been suggested in the past.
I'm proposing a version of template literals (documented in EMCAScript as follows: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals be implemented in Java.
This might also use the backtick/grave (`) key rather than the traditional double quotation mark ("), or it would remain the same.

Usage could be a number of possibilities but the one that sticks out to me would be something like the following:

String piece = "hello world";
String whole = "This is a ${piece} message!";

Alternatively:

String piece = "hello world";
String whole = "This is a {piece} message!";

or:

String piece = "hello world";
String whole = `This is a ${piece} message!`;

That I can think of, the best way to do this would be to compile it as String#format (https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)). This would ensure backwards compatibility.

I personally would absolutely love to see this implemented because it would make code much more readable and less cluttered with strings like "example " + variable + " and so on."



Comments
Agreed. I'm closing this out as a duplicate of JEP 430 String Templates (Preview). https://openjdk.org/jeps/430
06-09-2023

For me, this is a duplicate of https://bugs.openjdk.org/browse/JDK-8286021
03-09-2023

There seem compatibility issues here. Moved to JDK to get further evaluations.
17-05-2021