O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering Java 11 - Second Edition

Book Description

Update your Java knowledge with the latest features of Java 11, such as the low-Overhead Garbage Collector, Local-Variable Syntax for Lambda Parameters, and Dynamic Class-File Constants

Key Features

  • Explore the latest features in Java 9,Java 10, and Java 11
  • Enhance your Java application development and migration approaches
  • Full coverage of modular Java applications, G1 Garbage Collector, JMH

Book Description

Java 11 is a long-term release and its new features add to the richness of the language. It emphasizes variable-type inference, performance improvements, along with simplified multithreading.

The Java platform has a special emphasis on modularity, making this the programming platform of choice for millions of developers. The modern Java platform can be used to build robust software applications, including enterprise-level and mobile applications. Fully updated for Java 11, this book stands to help any Java developer enjoy the richness of the Java programming language.

Mastering Java 11 is your one-stop guide to fully understanding recent Java platform updates. It contains detailed explanations of the recent features introduced in Java 9, Java 10, and Java 11 along with obtaining practical guidance on how to apply the new features. As you make your way through the chapters, you'll discover further information on the developments of the Java platform and learn about the changes introduced by the variable handles and Project Coin, along with several enhancements in relation to import statements processing. In the concluding chapters, you'll learn to improve your development productivity, making your applications more efficient. You'll also be able to get to grips with the command-line flags with respect to various utilities and the command-line utility changes featured in the current Java platform. By the end of the book, you'll have obtained an advanced level understanding of the Java platform and its recent changes.

What you will learn

  • Write modular Java applications
  • Migrate existing Java applications to modular ones
  • Understand how the default G1 garbage collector works
  • Leverage the possibilities provided by the newly introduced Java Shell
  • Performance test your application effectively with the JVM harness
  • Learn how Java supports the HTTP 2.0 standard
  • Find out how to use the new Process API
  • Explore the additional enhancements and features of Java 9, 10, and 11

Who this book is for

Mastering Java 11 is for experienced Java developers with a solid understanding of the Java language and want to progress to an advanced level.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Java 11 Second Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. The Java 11 Landscape
    1. Technical requirements
    2. Understanding the Java platform's new versioning model
      1. Feature-driven releases
      2. Time-based releases
    3. Understanding the significance of Java 9
      1. Breaking the monolith
      2. Using the Java Shell
      3. Taking control of external processes
      4. Boosting performance with G1
      5. Measuring performance with JMH
      6. Getting ready for HTTP 2.0
      7. Encompassing reactive programming
    4. Benefiting from changes introduced with Java 10
      1. Local variable type inference
      2. Consolidation of the JDK forest into a single repository
      3. Garbage collection interface
      4. Parallel full garbage collector for G1
      5. Application class-data sharing
      6. Thread-local handshakes
      7. Removal of the native-header generation tool (javah)
      8. Additional Unicode language-tag extensions
      9. Heap allocation on alternative memory devices
      10. Experimental Java-based JIT compiler
      11. Root certificates
    5. Benefiting from changes introduced with Java 11
      1. Dynamic class-file constants
      2. Epsilon – an arbitrarily low-overhead garbage collector
      3. Removal of the Java EE and CORBA modules
      4. Local variable syntax for Lambda parameters
    6. Summary
    7. Questions
    8. Further reading
  8. Discovering Java 11
    1. Technical requirements
    2. Improved contended locking
      1. Improvement goals
    3. Segmented code cache
      1. Memory allocation
    4. Smart Java compilation
    5. Resolving lint and doclint warnings 
    6. Tiered attribution for Javac 
    7. Annotations pipeline 2.0 
    8. New version-string scheme 
    9. Generating runtime compiler tests automatically 
    10. Testing class-file attributes generated by Javac 
    11. Storing interned strings in class-data sharing archives 
      1. The problem
      2. The Java 9 solution
      3. The Java 10 improvement
        1. Class determination
        2. AppCDS archive creation
        3. Using the AppCDS archive
    12. Preparing JavaFX UI controls and Cascading Style Sheet APIs for modularization
      1. JavaFX overview
      2. Implications for Java 9, 10, and 11
    13. Compact strings 
    14. Merging selected Xerces 2.11.0 updates into JAXP 
    15. Updating JavaFX/Media to the newer version of GStreamer 
    16. HarfBuzz font-layout engine
    17. HiDPI graphics on Windows and Linux
    18. Marlin graphics renderer 
    19. Unicode 8.0.0
      1. New in Unicode 8.0.0
      2. Updated classes in Java 9
    20. Reserved stack areas for critical sections 
      1. The pre-Java 9 situation
      2. New in Java 9
    21. Dynamic linking of language-defined object models 
      1. Proof of concept
    22. Additional tests for humongous objects in G1 
    23. Improving test-failure troubleshooting 
      1. Environmental information
      2. Java process information
    24. Optimizing string concatenation 
    25. HotSpot C++ unit-test framework 
    26. Enabling GTK3 on Linux 
    27. New HotSpot build system 
    28. Consolidating the JDF forest into a single repository 
    29. Summary
    30. Questions
    31. Further reading
  9. Java 11 Fundamentals
    1. Technical requirements
    2. Working with variable handlers
      1. Working with the AtoMiC ToolKit
      2. Using the sun.misc.Unsafe class
    3. Import statement depreciation warnings
    4. Milling Project Coin
      1. Using the @SafeVarargs annotation
      2. The try-with-resource statement
      3. Using the diamond operator
      4. Discontinuing use of the underscore
      5. Making use of private interface methods
    5. Import statement processing
    6. Inferring local variables 
      1. Inferring declarations with the var identifier
      2. Local variable syntax for Lambda parameters
    7. Thread-local handshakes
    8. Heap allocation on alternative memory devices
    9. Root certificates
    10. Dynamic class-file constants
    11. Removal of the Java EE and CORBA modules
    12. Summary
    13. Questions
    14. Further reading
  10. Building Modular Applications with Java 11
    1. Technical requirements
    2. A modular primer
    3. The modular JDK
      1. Modular source code
      2. JDK source code organization before modularization
        1. Development tools
          1. Deployment
          2. Internationalization
          3. Monitoring
          4. RMI
          5. Security
          6. Troubleshooting
          7. Web services
        2. JavaFX tools
        3. Java runtime environment
        4. Source code
        5. Libraries
        6. C header files
        7. Database
      3. JDK source code reorganization
    4. Modular runtime images
      1. Adopting a runtime format 
      2. Runtime image restructure
      3. Supporting common operations
      4. Deprivileging JDK classes
      5. Preserving existing behaviors
    5. Module system
      1. Module paths
      2. Access-control boundary violations
      3. Runtime
    6. Modular Java application packaging
      1. An advanced look at the Java Linker
      2. Java Packager options
    7. The Java Linker
    8. Encapsulating most internal APIs
    9. Summary
    10. Questions
    11. Further reading
  11. Migrating Applications to Java 11
    1. Technical requirements
    2. A quick review of Project Jigsaw
      1. Classpath
      2. The monolithic nature of the JDK
    3. How modules fit into the Java landscape
      1. Base module
      2. Reliable configuration
      3. Strong encapsulation
    4. Migration planning
      1. Testing a simple Java application
      2. Potential migration issues
        1. The JRE
        2. Accessing internal APIs
        3. Accessing internal JARs
        4. JAR URL depreciation
        5. Extension mechanism
        6. The JDK's modularization
    5. Advice from Oracle
      1. Preparatory steps
      2. Getting the JDK early access build
      3. Running your program before recompiling
      4. Updating third-party libraries and tools
      5. Compiling your application
        1. Pre-Java 9 -source and -target options
        2. Java 10 and 11 -source and -target options
      6. Running jdeps on your code
      7. Breaking encapsulation
        1. The --add-opens option
        2. The --add-exports option
        3. The --permit-illegal-access option
    6. Runtime image changes
      1. Java version schema
      2. JDK and JRE's layout
      3. What has been removed?
      4. Updated garbage collection
    7. Deploying your applications
      1. Selecting your JRE version 
      2. Serialized applets
      3. JNLP update
        1. Nested resources
        2. FX XML extension
        3. JNLP file syntax
        4. Numeric version comparison
    8. Useful tools
      1. Java Environment -jEnv
      2. Maven
      3. Obtaining the M2Eclipse IDE
    9. Summary
    10. Questions
    11. Further reading
  12. Experimenting with the Java Shell
    1. Technical requirements
    2. Understanding JShell
    3. Getting started with JShell
    4. Practical uses of JShell
      1. Feedback modes
        1. Creating a custom feedback mode
      2. Listing your assets
      3. Editing in JShell
        1. Modifying text
        2. Basic navigation
        3. Historical navigation
        4. Advanced editing commands
    5. Working with scripts
      1. Startup scripts
      2. Loading scripts
      3. Saving scripts
      4. Advanced scripting with JShell
    6. Summary
    7. Questions
    8. Further reading
  13. Leveraging the Default G1 Garbage Collector
    1. Technical requirements
    2. Overview of garbage collection
      1. Object life cycle
        1. Object creation
        2. Object mid-life
        3. Object destruction
      2. Garbage collection algorithms
        1. Mark and sweep
        2. Concurrent Mark Sweep (CMS) garbage collection
        3. Serial garbage collection
        4. Parallel garbage collection
        5. G1 garbage collection
      3. Garbage collection options
      4. Java methods relevant to garbage collection
        1. The System.gc() method
        2. The finalize() method
    3. The pre-Java 9 garbage collection schema
      1. Visualizing garbage collection
      2. Garbage collection upgrades in Java 8
      3. Case study – games written with Java
    4. Collecting garbage with the new Java platform
      1. Default garbage collection 
      2. Depreciated garbage collection combinations
      3. Unified garbage collection logging
      4. Unified JVM logging
        1. Tags
        2. Levels
        3. Decorations
        4. Output
        5. Command-line options
      5. Unified GC logging
        1. Garbage collection logging options
        2. The gc tag
        3. Macros
        4. Additional considerations
      6. Garbage collection interface
      7. Parallel full garbage collection for G1
      8. Epsilon – an arbitrarily low-overhead GC
    5. Persistent issues
      1. Making objects eligible for garbage collection
    6. Summary
    7. Questions
    8. Further reading
  14. Microbenchmarking Applications with JMH
    1. Technical requirements
    2. Microbenchmarking overview
      1. Approach to using JMH
        1. Installing Java and Eclipse 
        2. Hands-on experiment
    3. Microbenchmarking with Maven
    4. Benchmarking options
      1. Modes
      2. Time units
    5. Techniques for avoiding microbenchmarking pitfalls
      1. Power management
      2. OS schedulers
      3. Timesharing
      4. Eliminating dead-code and constant folding
      5. Run-to-run variance
      6. Cache capacity
    6. Summary
    7. Questions
    8. Further reading
  15. Making Use of the Process API
    1. Technical requirements
    2. Introducing processes
    3. Working with the ProcessHandle interface
      1. Getting the PID of the current process
      2. Getting information about processes
      3. Listing processes
        1. Listing children
        2. Listing descendants
        3. Listing all processes
      4. Waiting for processes
      5. Terminating processes
    4. Reviewing a sample process controller app
      1. Main class
      2. Parameters class
      3. ParamsAndHandle
      4. ControlDaemon
    5. Summary
    6. Questions
    7. Further reading
  16. Fine-Grained Stack Tracing
    1. Technical requirements
    2. Overview of the Java Stack
    3. The importance of stack information
      1. Example – restricting callers
      2. Example – getting loggers for callers
    4. Working with StackWalker
      1. Getting an instance of StackWalker
      2. Enum options
        1. RETAIN_CLASS_REFERNCE
        2. SHOW_REFLECT_FRAMES
        3. SHOW_HIDDEN_FRAMES
        4. Final thoughts on enum constants
      3. Accessing classes
      4. Walking methods
    5. StackFrame
    6. Performance
    7. Summary
    8. Questions
  17. New Tools and Tool Enhancements
    1. Technical requirements
    2. Working with the HTTP client
      1. The pre-Java 9 HTTP client
      2. The Java 11 HTTP client
      3. Limitations of the HTTP client API
    3. Understanding Javadoc and the Doclet API
      1. The pre-Java 9 Doclet API
        1. API enums
        2. API classes
        3. API interfaces
      2. Problems with the pre-existing Doclet API
      3. Java 9's Doclet API
        1. Compiler tree API
        2. Language model API
          1. The AnnotatedConstruct interface
          2. The SourceVersion enum
          3. The UnknownEntityException exception
      4. Using the HTML5 Javadoc
      5. Javadoc search
        1. Introducing Camel Case search
    4. Changes to the Multiple JRE feature
    5. JavaScript Parser
      1. Nashorn
        1. Using Nashorn as a command-line tool
        2. Using Nashorn as an embedded interpreter
      2. ECMAScript
      3. Parser API
    6. Multiple-release JAR files
      1. Identifying multi-release JAR files
      2. Related JDK changes
    7. Java-level JVM Compiler Interface
      1. BeanInfo annotations 
        1. JavaBean
        2. BeanProperty
      2. SwingContainer
      3. BeanInfo classes
    8. TIFF support
    9. Platform logging
      1. The java.util.logging package
      2. Logging in the modern Java platform
    10. XML Catalogs
      1. The OASIS XML Catalog standard
      2. JAXP processors
      3. Earlier XML Catalogs
      4. Current XML Catalogs
    11. Collections
      1. Using collections prior to the modern Java platform
      2. Using new collection literals
    12. Platform-specific desktop features
    13. Enhanced method handling
      1. The reason for the enhancement
      2. Lookup functions
      3. Argument handling
      4. Additional combinations
    14. Enhanced depreciation
      1. What the @Deprecated annotation really means
    15. The native header generation tool (javah)
    16. Summary
    17. Questions
    18. Further reading
  18. Concurrency Enhancements
    1. Technical requirements
    2. Reactive programming
      1. Reactive programming standardization
    3. The Flow API
      1. The Flow.Publisher interface
      2. The Flow.Subscriber interface
      3. The Flow.Subscription interface
      4. The Flow.Processor interface
      5. Sample implementation
    4. Additional concurrency updates
      1. Java concurrency
        1. Concurrency explained
        2. System configurations
        3. Java threads
      2. Concurrency improvements
      3. CompletableFuture API enhancements
        1. Class details
        2. Enhancements
    5. Spin-wait hints
    6. Summary
    7. Questions
    8. Further reading
  19. Security Enhancements
    1. Technical requirements
    2. Datagram Transport Layer Security
      1. DTLS protocol version 1.0
      2. DTLS protocol version 1.2
      3. DTLS support in Java
    3. Creating PKCS12 keystores
      1. Keystore primer
      2. Java Keystore (JKS)
        1. Understanding the KeyStore.Builder
        2. The CallbackHandlerProtection class
        3. The PasswordProtection class
        4. The PrivateKeyEntry class
        5. The SecretKeyEntry class
        6. The TrustedCertificateEntry class
      3. PKCS12 default in Java 9, 10, and 11
    4. Improving security application performance
      1. Security policy enforcement
      2. Permission evaluation
      3. The java.Security.CodeSource package
      4. Package checking algorithm
    5. The TLS application-layer protocol negotiation extension
      1. TLS ALPN extension
      2. The javax.net.ssl package
      3. The java.net.ssl package extension
    6. Leveraging CPU instructions for GHASH and RSA
      1. Hashing
    7. OCSP stapling for TLS
      1. OCSP stapling primer
      2. Recent changes to the Java platform
    8. DRBG-based SecureRandom implementations
    9. Summary
    10. Questions
    11. Further reading
  20. Command-Line Flags
    1. Technical requirements
    2. Unified JVM logging
      1. Command-line options
      2. Decorations
      3. Levels
      4. Working with Xlog output
      5. Tags
    3. Compiler control
      1. Compilation modes
        1. The C1 compilation mode
        2. The C2 compilation mode
        3. Tiered compilation
      2. Compiler control in Java 11
    4. Diagnostic commands
    5. The heap profiling agent
    6. Removing your JHAT
    7. Command-line flag argument validation
    8. Compiling for older platform versions
    9. The experimental Java-based JIT compiler
    10. Summary
    11. Questions
    12. Further reading
  21. Additional Enhancements to the Java Platform
    1. Technical requirements
    2. Support for UTF-8
      1. The ResourceBundle class
        1. The nested class
        2. Fields and constructors
        3. Methods
      2. Changes in the modern Java platform
    3. Unicode support
      1. The java.lang package
      2. The java.text package
      3. Additional significance
    4. Linux/AArch64 port
    5. Multiresolution images
    6. Common Locale Data Repository
    7. Summary
    8. Questions
  22. Future Directions
    1. Technical requirements
    2. An overview of the JDK Enhancement Proposal
    3. JEP Candidates
      1. JEP 326: Raw String Literals
      2. JEP 334: JVM Constants API
      3. JEP 337: RDMA Network Sockets
      4. JEP 338: Vector API
      5. JEP 339: Edwards-Curve Digital Signature Algorithm
    4. JEP Submitted
    5. JEP Drafted
    6. Ongoing special projects
      1. Annotations Pipeline 2.0
      2. Audio Synthesis Engine
      3. Caciocavallo
      4. Common VM Interface
      5. Compiler Grammar
      6. Device I/O
      7. Graal
      8. HarfBuzz integration
      9. Kona
      10. OpenJFX
      11. Panama
      12. Shenandoah
    7. Summary
    8. Questions
  23. Contributing to the Java Platform
    1. Technical requirements
    2. The Java Community
    3. Participating in a Java User Group
    4. Java Community Process
    5. Oracle Technology Network
    6. Writing technical articles 
    7. Summary
    8. Questions
  24. Assessment
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think