In der Diskussion ist der „Back-Tick“, sodass es so aussehen würde:
File Paths Example
Traditional String Literals | Raw String Literals |
---|---|
Runtime.getRuntime().exec("\"C:\\Program Files\\foo\" bar"); |
Runtime.getRuntime().exec(`"C:\Program Files\foo" bar"`); |
Multi-line Example
Traditional String Literals | Raw String Literals |
---|---|
String html = "<html>\n" " <body>\n" + " <p>Hello World.</p>\n" + " </body>\n" + "</html>\n"; |
String html = `<html> <body> <p>Hello World.</p> </body> </html> `; |
Regular Expression Example
Traditional String Literals | Raw String Literals |
---|---|
System.out.println("this".matches("\\w\\w\\w\\w")); |
System.out.println("this".matches(`\w\w\w\w`)); |
Output:
true
Polyglot Example
Traditional String Literals | Raw String Literals |
---|---|
String script = "function hello() {\n" + " print(\'\"Hello World\"\');\n" + "}\n" + "\n" + "hello();\n"; ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); Object obj = engine.eval(script); |
String script = `function hello() { print('"Hello World"'); }
ScriptEngine engine = new ScriptEngineManager().getEngineByName(„js“); Object obj = engine.eval(script); |
Output:
"Hello World"
Database Example
Traditional String Literals | Raw String Literals |
---|---|
String query = "SELECT EMP_ID, LAST_NAME\n" + "FROM EMPLOYEE_TBL\n" + "WHERE CITY = 'INDIANAPOLIS'\n" + "ORDER BY EMP_ID, LAST_NAME;\n"; |
String query = `SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TB WHERE CITY = 'INDIANAPOLIS' ORDER BY EMP_ID, LAST_NAME; `; |
Ich bin gespannt, ob das in Java 10 kommen wird. Ich dachte schon, dass Java 10 Release wird eher klein, doch zusammen mit den var-Variablen sind das zwei kleine, aber bedeutende Sprachänderungen.
Details unter http://openjdk.java.net/jeps/8196004.