-
Notifications
You must be signed in to change notification settings - Fork 0
Java 13
Release Date: September 17, 2019 | Type: Standard (non-LTS) | Status:
⚠️ End of Life
Java 13 refines two previews from the previous release: switch expressions gain the yield keyword, and a brand-new preview arrives — text blocks, ending decades of string-concatenation pain for multi-line text (SQL, JSON, HTML).
flowchart LR
A["String s = 'line1\\n'\n+ 'line2\\n'\n+ 'line3';"] -->|JEP 355 Preview| B["String s = '''\n line1\n line2\n line3\n ''';"]
What: Multi-line string literals delimited by """, with smart, automatic indentation stripping.
// Before: escape hell for a JSON payload
String json = "{\n" +
" \"name\": \"Neelu\",\n" +
" \"role\": \"Java Architect\"\n" +
"}";
// Java 13 text block
String json = """
{
"name": "Neelu",
"role": "Java Architect"
}
""";
// SQL example — no more string concatenation for multi-line queries
String query = """
SELECT id, name, salary
FROM employees
WHERE department = ?
ORDER BY salary DESC
""";Indentation rules — the compiler strips the common minimum leading whitespace based on the closing """ position:
String html = """
<html>
<body>
<p>Hello</p>
</body>
</html>
""";
// Indentation relative to the closing delimiter is preserved,
// common leading whitespace across all lines is strippedflowchart TD
A["Raw text block source"] --> B{"Find minimum\nindentation across\nall non-blank lines"}
B --> C["Strip that amount\nfrom every line"]
C --> D["Trailing whitespace\nremoved per line"]
D --> E["Final String value"]
int result = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
default -> {
int len = day.toString().length();
yield len; // yield replaces break-with-value from the 1st preview
}
};yield was introduced specifically because break value; (used in the Java 12 preview) was ambiguous and confusing next to labeled break.
What: Extends AppCDS (Java 10) so you no longer need a separate training run to build the class list — the archive can be created automatically at JVM exit.
# Single command — archive created automatically when the JVM exits normally
java -XX:ArchiveClassesAtExit=app-dynamic.jsa -cp app.jar Main
# Subsequent runs use it for faster startup
java -XX:SharedArchiveFile=app-dynamic.jsa -cp app.jar MainZGC (introduced experimentally in Java 11) can now return unused heap memory to the OS instead of holding onto it — important for containerized/cloud deployments where memory is billed.
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xmx4g -jar app.jar✅ Reach for text blocks anywhere you have SQL, JSON, HTML, or multi-line log messages hardcoded in Java
✅ Use yield (not break value) once on Java 13+ previews — it's the syntax that stuck
❌ Don't rely on exact preview syntax in production code — text blocks changed slightly before standardizing in Java 15
// Text blocks + String.formatted() (Java 15+) is a killer combo for readable templates
String message = """
Hello, %s!
You have %d new notifications.
""".formatted(userName, count);