Overview
This module covers advanced Java features from Java 9 onwards, including modules, var keyword, records, sealed classes, pattern matching, text blocks, and other modern Java enhancements.
Learning Objectives
- Understand Java Module System (Java 9+)
- Learn var keyword and local variable type inference
- Master records for data classes
- Work with sealed classes and interfaces
- Understand pattern matching
- Use text blocks and modern syntax
- Learn switch expressions and enhancements
Topics Covered
17.1 Java Module System (Java 9+)
17.1.1 Introduction to Modules
- Platform Modularity
- Project Jigsaw
- Benefits of Modules
- Strong Encapsulation
- Reliable Configuration
17.1.2 Module Basics
- module-info.java
- Module Declaration
- Module Path vs Classpath
- Named Modules
- Automatic Modules
- Unnamed Module
17.1.3 Module Directives
- requires
- exports
- opens
- uses
- provides...with
- transitive Dependencies
17.1.4 Creating Modules
- Module Structure
- Module Compilation
- Module Packaging
- Running Modules
- Multi-Module Projects
17.1.5 Module Resolution
- Readability
- Accessibility
- Module Graph
- Dependencies
17.1.6 Migration to Modules
- Bottom-Up Migration
- Top-Down Migration
- Automatic Modules
- Unnamed Module
- Split Packages
17.2 Local Variable Type Inference (Java 10+)
17.2.1 var Keyword
- Type Inference
- Local Variables Only
- Improved Readability
- Diamond Operator Enhancement
17.2.2 var Usage Rules
- Must Have Initializer
- Cannot Be null
- Cannot Be Used for Fields
- Cannot Be Used for Method Parameters
- Cannot Be Used for Method Return Types
17.2.3 var Best Practices
- When to Use var
- Maintaining Readability
- Naming Variables
- Complex Type Expressions
17.2.4 var with Collections
- Generic Type Inference
- Diamond Operator
- Stream Operations
- Lambda Expressions
17.3 Records (Java 14+)
17.3.1 Understanding Records
- Data Carrier Classes
- Immutable Data
- Compact Syntax
- Reduced Boilerplate
17.3.2 Record Declaration
- record Keyword
- Components
- Automatic Members
- Constructor, Getters, equals, hashCode, toString
17.3.3 Record Features
- Compact Constructor
- Canonical Constructor
- Custom Methods
- Static Members
- Nested Records
17.3.4 Record Restrictions
- Implicit final
- extends java.lang.Record
- Cannot Extend Other Classes
- Can Implement Interfaces
- All Fields Are final
17.3.5 Records vs Classes
- When to Use Records
- Immutability
- Data Transfer Objects
- Value Objects
17.4 Sealed Classes (Java 17+)
17.4.1 Understanding Sealed Classes
- Controlled Inheritance
- sealed Keyword
- permits Clause
- Exhaustive Hierarchies
17.4.2 Sealed Class Declaration
- sealed Modifier
- Permitted Subclasses
- Same Module/Package
- Compiler Enforcement
17.4.3 Permitted Subclasses
- final Classes
- sealed Classes
- non-sealed Classes
- Must Be Accessible
17.4.4 Sealed Interfaces
- sealed Interface
- Permitted Implementations
- Type Safety
- Pattern Matching
17.4.5 Use Cases
- Domain Modeling
- Algebraic Data Types
- Pattern Matching
- API Design
17.5 Pattern Matching
17.5.1 Pattern Matching for instanceof (Java 16+)
- Type Pattern
- Pattern Variable
- Scope
- Negation
17.5.2 Pattern Matching for switch (Java 17+)
- Type Patterns in switch
- Guarded Patterns
- Null Handling
- Exhaustiveness
17.5.3 Record Patterns (Java 19+)
- Deconstructing Records
- Nested Patterns
- Component Access
- Use Cases
17.6 Switch Expressions (Java 14+)
17.6.1 Enhanced switch
- Expression vs Statement
- Arrow Syntax (->)
- Yield Keyword
- Multiple Labels
17.6.2 Switch Expression Features
- Returns Value
- No Fall-Through
- Exhaustiveness
- Type Safety
17.6.3 Pattern Matching in switch
- Type Patterns
- Guarded Patterns
- Case Refinement
- Sealed Types
17.7 Text Blocks (Java 15+)
17.7.1 Understanding Text Blocks
- Multi-Line Strings
- Triple Quotes (""")
- Improved Readability
- Incidental Whitespace
17.7.2 Text Block Features
- Automatic Indentation
- Escape Sequences
- String Interpolation Alternative
- SQL, JSON, HTML
17.7.3 Text Block Methods
- stripIndent()
- translateEscapes()
- formatted()
- Use Cases
17.8 Stream API Enhancements
17.8.1 Java 9 Additions
- takeWhile()
- dropWhile()
- ofNullable()
- iterate() with Predicate
17.8.2 Java 16 Additions
- toList()
- mapMulti()
17.8.3 Java 21+ Additions
- Additional Stream Operations
- Performance Improvements
17.9 Collection Factory Methods (Java 9+)
17.9.1 Immutable Collections
- List.of()
- Set.of()
- Map.of()
- Map.ofEntries()
17.9.2 Characteristics
- Null Elements Not Allowed
- Immutable
- Space Efficient
- Compact Representation
17.10 Optional Enhancements
17.10.1 Java 9 Additions
- stream()
- ifPresentOrElse()
- or()
17.10.2 Java 10 Additions
- orElseThrow()
17.10.3 Java 11 Additions
- isEmpty()
17.11 String Enhancements
17.11.1 Java 11 Methods
- isBlank()
- lines()
- strip(), stripLeading(), stripTrailing()
- repeat()
17.11.2 Java 12+ Methods
- indent()
- transform()
17.11.3 Java 15+ Methods
- formatted()
- Text Blocks Support
17.12 HTTP Client (Java 11+)
17.12.1 New HTTP Client API
- HttpClient
- HttpRequest
- HttpResponse
- Modern API
17.12.2 Creating Requests
- Builder Pattern
- GET, POST, etc.
- Headers
- Body Publishers
17.12.3 Sending Requests
- Synchronous
- Asynchronous
- CompletableFuture
- Response Handling
17.12.4 HTTP/2 Support
- Protocol Version
- Server Push
- Multiplexing
- Performance
17.13 Process API Enhancements (Java 9+)
17.13.1 ProcessHandle
- Process Information
- Process Control
- PID Access
- Process Tree
17.13.2 Process Management
- descendants()
- children()
- onExit()
- Monitoring
17.14 Reactive Streams (Java 9+)
17.14.1 Flow API
- Publisher
- Subscriber
- Subscription
- Processor
17.14.2 Backpressure
- Flow Control
- Demand Management
- Buffering
17.15 Private Interface Methods (Java 9+)
17.15.1 Private Methods in Interfaces
- Code Reuse
- Encapsulation
- Helper Methods
- Private Static Methods
17.16 Try-With-Resources Enhancement (Java 9+)
17.16.1 Effectively Final Variables
- Resource Variable Declaration
- Simplified Syntax
- Use Cases
17.17 Diamond Operator Enhancement (Java 9+)
17.17.1 Anonymous Classes
- Type Inference
- Generic Type Inference
- Syntax Improvement
17.18 @SafeVarargs Enhancement (Java 9+)
17.18.1 Private Methods
- Annotation Usage
- Varargs Safety
- Compiler Warnings
17.19 CompletableFuture Enhancements
17.19.1 Java 9 Additions
- Timeout Support
- Delay Execution
- orTimeout()
- completeOnTimeout()
17.19.2 Copy Methods
- copy()
- minimalCompletionStage()
17.20 Stack Walking API (Java 9+)
17.20.1 StackWalker
- Efficient Stack Traversal
- Lazy Evaluation
- Filtering
- Stack Frame Access
17.21 VarHandle (Java 9+)
17.21.1 Understanding VarHandle
- Variable Handles
- Memory Ordering
- Atomic Operations
- Low-Level Access
17.22 Method Handles Enhancements
17.22.1 MethodHandle API
- Dynamic Method Invocation
- Performance
- Reflection Alternative
17.23 Nested Based Access Control (Java 11+)
17.23.1 Nest Members
- Private Member Access
- Nested Classes
- JVM Support
- Compiler Changes
17.24 Dynamic Class-File Constants (Java 11+)
17.24.1 CONSTANT_Dynamic
- Constant Pool
- Bootstrap Methods
- Performance
17.25 Epsilon GC (Java 11+)
17.25.1 No-Op Garbage Collector
- Testing
- Short-Lived Applications
- Performance Testing
17.26 Z Garbage Collector (Java 11+)
17.26.1 ZGC
- Low Latency
- Scalability
- Concurrent GC
- Large Heaps
17.27 Virtual Threads (Java 21+)
17.27.1 Project Loom
- Lightweight Threads
- High Scalability
- Simple Concurrency Model
- Virtual vs Platform Threads
17.27.2 Creating Virtual Threads
- Thread.ofVirtual()
- Executors.newVirtualThreadPerTaskExecutor()
- Use Cases
17.28 Sequenced Collections (Java 21+)
17.28.1 SequencedCollection Interface
- First and Last Elements
- Reversed Views
- Ordering
- New Methods
17.29 String Templates (Java 21+)
17.29.1 Template Expressions
- String Interpolation
- Type Safety
- STR Processor
- Custom Processors
17.30 Foreign Function & Memory API (Java 22+)
17.30.1 FFM API
- Native Code Interop
- Memory Access
- Panama Project
- Safety
Hands-on Exercises
- Create modular applications
- Use var for type inference
- Implement records for data classes
- Create sealed class hierarchies
- Practice pattern matching
- Use text blocks for multi-line strings
- Implement HTTP client examples
- Create switch expressions
- Work with immutable collections
- Build reactive streams examples
- Use virtual threads
- Implement modern Java patterns
Key Takeaways
- Modules provide strong encapsulation
- var improves code readability
- Records reduce boilerplate
- Sealed classes control inheritance
- Pattern matching simplifies code
- Text blocks improve multi-line strings
- Virtual threads enable high concurrency
Common Mistakes to Avoid
- Overusing var
- Wrong module configuration
- Not understanding sealed classes
- Ignoring pattern matching benefits
- Misusing records
- Complex text block indentation
- Not leveraging modern features
Real-World Applications
- Microservices architecture
- Data transfer objects
- Domain modeling
- API clients
- Concurrent applications
- Modern enterprise apps
- Cloud-native applications
Additional Resources
- Inside Java (Blog)
- Java Language Updates
- OpenJDK Documentation
- Modern Java in Action
- Effective Java (Latest Edition)
Assessment
- Quiz on modern Java features
- Practical: Module system exercises
- Records and sealed classes projects
- Pattern matching challenges
- Virtual threads implementations