Detailed Breakdown of Key Java Features
Modules (Java 9+)
Modules provide a way to encapsulate code at a higher level than packages, helping organize large applications.
// In module-info.java
module com.myapp.core {
requires java.sql;
exports com.myapp.core.api;
provides com.myapp.core.spi.Service with com.myapp.core.impl.ServiceImpl;
}
Benefits:
- Strong encapsulation beyond package-level
- Explicit dependencies between components
- Improved application performance and security
- Smaller runtime footprints with custom JRE creation
Records (Java 16+)
Records are immutable data carriers that reduce boilerplate code for simple data classes.
public record Person(String name, int age) {
// Compiler automatically generates:
// - Constructor
// - Accessor methods (name(), age())
// - equals(), hashCode(), toString()
// You can add validation:
public Person {
if (age < 0) throw new IllegalArgumentException("Age cannot be negative");
}
}
Benefits:
- Concise data class declaration
- Immutability by default
- Automatic implementations of common methods
Pattern Matching (Java 17+)
Simplifies conditional logic with more powerful instanceof operations and switch expressions.
// Pattern matching with instanceof
if (obj instanceof String s) {
// Can use 's' directly here without casting
System.out.println(s.length());
}
// Pattern matching with switch (preview)
Object obj = getValue();
String result = switch (obj) {
case Integer i -> "Integer: " + i;
case String s -> "String: " + s;
case null -> "null";
default -> "Unknown";
};
Benefits:
- More concise conditional code
- Type-safe pattern testing and variable binding
- Enhanced switch expressions
Text Blocks (Java 15+)
Multi-line string literals that preserve formatting without escape sequences.
String json = """
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
""";
Benefits:
- Improved readability for multi-line strings
- No need for most escape sequences
- Better representation of formatted text like HTML, JSON, SQL
I/O and NIO
Java provides robust APIs for input/output operations and networking.
// Traditional I/O
try (FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
// NIO (New I/O)
Path path = Paths.get("input.txt");
List<String> lines = Files.readAllLines(path);
// Networking
URL url = new URL("https://example.com");
try (InputStream is = url.openStream()) {
// Read from website
}
Benefits:
- Comprehensive file operations
- Platform-independent path manipulation
- Non-blocking I/O for high-performance applications
- Channel-based operations for efficient data transfer
Sealed Classes (Java 17+)
Restrict which classes can extend a class or implement an interface.
public sealed class Shape permits Circle, Rectangle, Triangle {
// Common shape functionality
}
public final class Circle extends Shape {
// Circle-specific code
}
public final class Rectangle extends Shape {
// Rectangle-specific code
}
public final class Triangle extends Shape {
// Triangle-specific code
}
Benefits:
- Control class hierarchy design
- Better pattern matching exhaustiveness
- Clear API design intent
JIT Compilation
Just-In-Time compilation optimizes Java bytecode at runtime.
How it works:
- Java code is compiled to bytecode (.class files)
- JVM initially interprets the bytecode
- JIT compiler identifies frequently executed code ("hot spots")
- Hot spots are compiled to native machine code for faster execution
- Adaptive optimization improves performance over time
Benefits:
- Improved execution speed
- Runtime optimization based on actual usage patterns
- Platform-specific optimizations
Optimized Collections
Java provides highly efficient implementations of common data structures.
// ArrayList - dynamic array
List<String> list = new ArrayList<>();
// HashMap - key-value storage
Map<String, Integer> map = new HashMap<>();
// Specialized collections
Queue<Task> tasks = new PriorityQueue<>();
Set<String> uniqueWords = new HashSet<>();
Optimizations include:
- Dynamic resizing
- Amortized constant-time operations
- Thread-safe variants (ConcurrentHashMap, etc.)
- Specialized implementations (EnumSet, ArrayDeque)
CompletableFuture
Enables asynchronous programming with composition capabilities.
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// This runs in a different thread
return fetchDataFromRemoteAPI();
})
.thenApply(data -> {
// Transform the data
return processData(data);
})
.thenCombine(otherFuture, (result1, result2) -> {
// Combine with another async operation
return combineResults(result1, result2);
});
// Handle completion
future.whenComplete((result, error) -> {
if (error != null) {
handleError(error);
} else {
useResult(result);
}
});
Benefits:
- Non-blocking operations
- Composable async operations
- Elegant error handling
- Thread management abstraction
Security Manager
Controls what operations applications can perform.
// Enable Security Manager
System.setSecurityManager(new SecurityManager());
// Define security policy in policy file
// grant permissions to specific code sources
Capabilities:
- Control file system access
- Restrict network operations
- Limit reflection usage
- Control thread creation and manipulation
Cryptography APIs
Java provides extensive support for encryption and security.
// Generate a key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
// Encrypt data
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(originalData);
// Message digests
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(message.getBytes());
Features:
- Symmetric and asymmetric encryption
- Digital signatures
- Secure random number generation
- Certificate management
Access Control
Java provides multiple layers of security through class loaders and access modifiers.
Class Loaders:
- Load classes dynamically at runtime
- Enforce namespace separation
- Validate bytecode
- Support custom class loading policies
Access modifiers:
public: Accessible everywhereprotected: Accessible in the package and subclasses- Default (no modifier): Accessible only in the package
private: Accessible only in the class
These security mechanisms work together to create a robust platform for developing secure applications.