A comprehensive guide to all Java 13 concepts with practical examples for interview preparation.
- Text Blocks (Preview)
- Switch Expressions (Second Preview)
- Reimplementation of the Legacy Socket API
- Dynamic CDS Archives
- FileSystems.newFileSystem()
- DOM and SAX Factories
- ZGC Improvements
- Common Interview Questions
Multi-line string literals for better readability.
// Before Java 13
String html = "<html>\n" +
" <body>\n" +
" <p>Hello, World!</p>\n" +
" </body>\n" +
"</html>";
// Java 13 - Text Blocks
String html = """
<html>
<body>
<p>Hello, World!</p>
</body>
</html>
""";
// Preserves formatting
String json = """
{
"name": "John",
"age": 30,
"city": "New York"
}
""";// Escape quotes
String text = """
She said "Hello"
""";
// Escape newline (suppress line break)
String text2 = """
Line 1 \
Line 2
"""; // Results in "Line 1 Line 2"
// Escape sequence
String text3 = """
Tab:\t
Newline:\n
""";// Common indentation is removed
String code = """
public void method() {
System.out.println("Hello");
}
""";
// If content is indented differently
String code2 = """
public void method() {
System.out.println("Hello");
}
""".indent(4); // Add 4 spaces indentation// Note: String interpolation comes later
// For now, use concatenation or formatting
String name = "John";
String message = """
Hello, %s!
""".formatted(name);Refinements to switch expressions based on feedback.
// Same as Java 12, with refinements
int result = switch (value) {
case 1 -> 10;
case 2 -> 20;
default -> {
System.out.println("Default case");
yield 0;
}
};Java 13 reimplements the legacy socket API to improve performance, scalability, and maintainability.
The socket API has been rewritten to:
- Improve performance and scalability
- Better maintainability
- Enhanced reliability
- No breaking changes to existing code
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
// Server example
try (ServerSocket serverSocket = new ServerSocket(8080)) {
System.out.println("Server listening on port 8080");
while (true) {
Socket socket = serverSocket.accept();
// Handle client connection
}
} catch (IOException ex) {
ex.printStackTrace();
}
// Client example
try (Socket socket = new Socket("localhost", 8080)) {
// Use socket for communication
} catch (IOException ex) {
ex.printStackTrace();
}- Better performance
- Improved scalability
- Enhanced maintainability
- Backward compatible (no code changes needed)
Dynamic Class-Data Sharing (CDS) Archives allow dynamic archiving of classes at application exit.
Dynamic CDS extends the existing CDS feature by allowing classes to be archived dynamically at runtime, not just during JDK build time.
# Create dynamic archive at application exit
java -XX:ArchiveClassesAtExit=app-cds.jsa -cp myApp.jar com.example.MyApp# Use the dynamic archive in subsequent runs
java -XX:SharedArchiveFile=app-cds.jsa -cp myApp.jar com.example.MyApp- Faster application startup
- Reduced memory footprint
- Dynamic class archiving (no rebuild needed)
- Works with application-specific classes
- Microservices with frequent restarts
- Applications with long startup times
- Memory-constrained environments
- Development environments (faster iteration)
Note: For detailed documentation and examples, see Dynamic CDS Archives Guide.
New method for creating file systems, particularly useful for working with ZIP files as file systems.
The FileSystems.newFileSystem() method allows you to create file systems from various sources, most commonly ZIP/JAR files.
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
// Create file system from ZIP file
Path zipPath = Paths.get("archive.zip");
Map<String, String> env = new HashMap<>();
env.put("create", "true");
try (FileSystem zipFs = FileSystems.newFileSystem(
URI.create("jar:" + zipPath.toUri()), env)) {
// Access files within ZIP
Path fileInZip = zipFs.getPath("file.txt");
String content = Files.readString(fileInZip);
// Or write to ZIP
Path newFile = zipFs.getPath("newfile.txt");
Files.writeString(newFile, "Hello from ZIP!");
}- Treat ZIP/JAR files as file systems
- Use standard Files API
- No need for special ZIP libraries
- Cleaner code
- Better integration with NIO.2
New factory methods for DOM and SAX parsers.
Java 13 introduces newDefaultInstance() methods for XML parser factories, providing explicit default instance creation.
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParserFactory;
// DOM Parser
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
// SAX Parser
SAXParserFactory saxFactory =
SAXParserFactory.newDefaultInstance();
SAXParser parser = saxFactory.newSAXParser();Old way:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
SAXParserFactory saxFactory = SAXParserFactory.newInstance();New way (Java 13+):
DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance();
SAXParserFactory saxFactory = SAXParserFactory.newDefaultInstance();- Explicit default instance creation
- Better clarity in code
- Consistent API design
- Future-proof API
Z Garbage Collector improvements for better memory management.
ZGC can now return unused heap memory to the operating system.
# Enable ZGC with uncommit delay
java -XX:+UnlockExperimentalVMOptions \
-XX:+UseZGC \
-XX:ZUncommitDelay=300 \
-cp myApp.jar com.example.MyApp-XX:+UseZGC: Enable Z Garbage Collector-XX:ZUncommitDelay=<seconds>: Delay before uncommitting unused memory (default: 300)-XX:+UnlockExperimentalVMOptions: Required for experimental features
- Better memory utilization
- Return unused memory to OS
- Improved resource efficiency
- Better performance for large heaps
- Applications with varying memory needs
- Large heap sizes
- Memory-constrained environments
- Long-running applications
A: Text Blocks provide multi-line string literals:
- Better readability for multi-line strings
- Preserves formatting
- Easier to write HTML, JSON, SQL, etc.
- Reduces escape sequences needed
A: Common indentation is automatically removed. The leftmost non-whitespace character determines the baseline. All lines are adjusted relative to this baseline.
Last Updated: 2024
Version: 1.0