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 9

Book Description

Your road to becoming a Java Ninja begins here!

About This Book

  • This book will teach you to build highly scalable, fast, and secure applications
  • It covers major concepts introduced with the new version of Java 9, which includes modular programming, HTTP 2.0, API changes, and more
  • It will guide you with tools, techniques and best practices to enhance application development

Who This Book Is For

This book is for enterprise developers and existing Java developers. Basic knowledge of Java would help.

What You Will Learn

  • Write modular Java applications in terms of the newly introduced module system
  • Migrate existing Java applications to modular ones
  • Understand how to use the G1 garbage collector in order to leverage the performance of your applications
  • Leverage the possibilities provided the newly introduced Java shell
  • Test your application's effectiveness with the JVM harness
  • See how Java 9 provides support for the http 2.0 standard
  • Use the new process API
  • Discover additional enhancements and features provided by Java 9

In Detail

Java 9 and its new features add to the richness of the language, one of the languages most used by developers to build robust software applications. Java 9 comes with a special emphasis on modularity with its integration with Jigsaw. This would be your one-stop guide to mastering the language.

You'll be provided with an overview and explanation of the new features introduced in Java 9 and the importance of the new APIs and enhancements. Some of the new features of Java 9 are ground-breaking and if you are an experienced programmer, you will be able to make your enterprise application leaner by learning these new features. You'll be provided with practical guidance in applying the newly acquired knowledge in regards to Java 9 and further information on future developments of the Java platform. This book will improve your productivity, making your application faster. By learning the best practices in Java, you'll become the ?go-to? person in your organization.

By the end of the book, you'll not only know the important concepts of Java 9, but you'll also have a nuanced understanding of the important aspects of programming with this great language.

Style and approach

Concepts and new terminology are explained in simple step-by-step manner. We cover a lot of real-world examples and case studies that will improve your Java productivity. This book covers new features on Java 9 and the much talked about Jigsaw integration.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. The Java 9 Landscape
    1. Java 9 at 20,000 feet
    2. Breaking the monolith
    3. Playing around with the Java Shell
    4. Taking control of external processes
    5. Boosting performance with G1
    6. Measuring performance with JMH
    7. Getting started with HTTP 2.0
    8. Encompassing reactive programming
    9. Expanding the wish list
    10. Summary
  3. Discovering Java 9
    1. Improved Contended Locking [JEP 143]
      1. Improvement goals
    2. Segmented code cache [JEP 197]
      1. Memory allocation
    3. Smart Java compilation, phase two [JEP 199]
    4. Resolving Lint and Doclint warnings [JEP 212]
    5. Tiered attribution for javac [JEP 215]
    6. Annotations pipeline 2.0 [JEP 217]
    7. New version-string scheme [JEP 223]
    8. Generating run-time compiler tests automatically [JEP 233]
    9. Testing class-file attributes generated by Javac [JEP 235]
    10. Storing interned strings in CDS archives [JEP 250]
      1. The problem
      2. The solution
    11. Preparing JavaFX UI controls and CSS APIs for modularization [JEP 253]
      1. JavaFX overview
      2. Implications for Java 9
    12. Compact strings [JEP 254]
      1. Pre-Java 9 status
      2. New with Java 9
    13. Merging selected Xerces 2.11.0 updates into JAXP [JEP 255]
    14. Updating JavaFX/Media to newer version of GStreamer [JEP 257]
    15. HarfBuzz Font-Layout Engine [JEP 258]
    16. HiDPI graphics on Windows and Linux [JEP 263]
    17. Marlin graphics renderer [JEP 265]
    18. Unicode 8.0.0 [JEP 267]
      1. New in Unicode 8.0.0
      2. Updated Classes in Java 9
    19. Reserved stack areas for critical sections [JEP 270]
      1. The pre-Java 9 situation
      2. New in Java 9
    20. Dynamic linking of language-defined object models [JEP 276]
      1. Proof of concept
    21. Additional tests for humongous objects in G1 [JEP 278]
    22. Improving test-failure troubleshooting [JEP 279]
      1. Environmental information
      2. Java process information
    23. Optimizing string concatenation [JEP 280]
    24. HotSpot C++ unit-test framework [JEP 281]
    25. Enabling GTK 3 on Linux [JEP 283]
    26. New HotSpot build system [JEP 284]
    27. Summary
  4. Java 9 Language Enhancements
    1. Working with variable handlers [JEP 193]
      1. Working with the AtoMiC Toolkit
      2. Using the sun.misc.Unsafe class
    2. Eliding depreciation warnings on import statements [JEP 211]
    3. Milling Project Coin [JEP 213]
      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
    4. Processing import statements correctly [JEP 216]
    5. Summary
  5. Building Modular Applications with Java 9
    1. A modular primer
    2. Reviewing Java's platform module system [JEP-200]
    3. Modularizing JDK source code [JEP-201]
      1. Pre-Java 9 JDK source code organization
        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
      2. JDK source code reorganized
    4. Understanding modular run-time images [JEP-220]
      1. Runtime format adoption
      2. Runtime image restructure
      3. Supporting common operations
      4. De-privileging JDK classes
      5. Preserving existing behaviors
    5. Getting to know the module system [JEP-261]
      1. Module paths
      2. Access-control boundary violations
        1. Runtime
    6. Modular Java application packaging [JEP-275]
      1. Advanced look at the Java Linker
      2. Java Packager options
    7. JLink - The Java Linker [JEP-282]
    8. Encapsulating most internal APIs [JEP-260]
    9. Summary
  6. Migrating Applications to Java 9
    1. Quick review of Project Jigsaw
      1. Classpath
      2. The monolithic nature of the JDK
    2. How modules fit into the Java landscape
      1. Base module
      2. Reliable configuration
      3. Strong encapsulation
    3. Migration planning
      1. Testing a simple Java application
      2. Potential migration issues
        1. The JRE
        2. Access to internal APIs
        3. Accessing internal JARs
        4. JAR URL depreciation
        5. Extension mechanism
        6. The JDK's modularization
    4. Advice from Oracle
      1. Preparatory steps
        1. Getting the JDK 9 early access build
        2. Running your program before recompiling
        3. Updating third-party libraries and tools
        4. Compiling your application
          1. Pre-Java 9 -source and -target options
          2. Java 9 -source and -target options
        5. Running jdeps on your code
      2. Breaking encapsulation
        1. The --add-opens option
        2. The --add-exports option
        3. The --permit-illegal-access option
      3. Runtime image changes
        1. Java version schema
        2. JDK and JRE layout
      4. What has been removed
      5. Updated garbage collection
      6. Deployment
        1. JRE version selection
        2. Serialized applets
        3. JNLP update
          1. Nested resources
          2. FX XML extension
          3. JNLP file syntax
          4. Numeric version comparison
    5. Useful tools
      1. Java environment - jEnv
      2. Maven
        1. Obtaining the M2Eclipse IDE
    6. Summary
  7. Experimenting with the Java Shell
    1. What is JShell?
    2. Getting Started with JShell
    3. Practical uses of JShell
      1. Feedback modes
        1. Creating a custom feedback mode
      2. Listing your assets
      3. Editing in the JShell
        1. Modifying text
        2. Basic navigation
        3. Historical navigation
        4. Advanced editing commands
    4. Working with scripts
      1. Start up scripts
      2. Loading scripts
      3. Saving scripts
      4. Advanced scripting with JShell
    5. Summary
  8. Leveraging the New Default G1 Garbage Collector
    1. 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
    2. Pre-Java 9 garbage collection
      1. Visualizing garbage collection
      2. Garbage collection upgrades in Java 8
      3. Case study - Games written with Java
    3. Collecting garbage with the new Java platform
      1. Default garbage collection
      2. Depreciated garbage collection combinations
      3. Unified garbage collection logging
        1. Unified JVM logging (JEP-158)
          1. Tags
          2. Levels
          3. Decorations
          4. Output
          5. Command-line options
        2. Unified GC logging (JEP-271)
          1. Garbage collection logging options
          2. The gc tag
          3. Macros
          4. Additional considerations
    4. Persistent issues
      1. Making objects eligible for garbage collection
    5. Summary
  9. Microbenchmarking Applications with JMH
    1. Microbenchmarking overview
      1. Approach to using JMH
        1. Installing Java 9 and Eclipse with Java 9 support
        2. Hands-on experiment
    2. Microbenchmarking with Maven
    3. Benchmarking options
      1. Modes
      2. Time units
    4. Techniques for avoiding microbenchmarking pitfalls
      1. Power management
      2. OS schedulers
      3. Time sharing
      4. Eliminating dead-code and constant folding
      5. Run-to-run variance
      6. Cache capacity
    5. Summary
  10. Making Use of the ProcessHandle API
    1. What are processes?
    2. The new ProcessHandle interface
    3. Getting the PID of the current process
    4. Getting information about a process
    5. Listing processes
      1. Listing children
      2. Listing descendants
      3. Listing all processes
    6. Waiting for processes
    7. Terminating processes
    8. A small process controller application
      1. Main class
      2. Parameters class
      3. The ParamsAndHandle class
      4. The ControlDaemon class
    9. Summary
  11. Fine-Grained Stack Tracing
    1. Overview of the Java Stack
    2. The importance of stack information
      1. Example - Restricting callers
      2. Example - Getting logger for caller
    3. Working with StackWalker
      1. Getting an instance of StackWalker
        1. RETAIN_CLASS_REFERENCE
        2. SHOW_REFLECT_FRAMES
        3. SHOW_HIDDEN_FRAMES
        4. Final thoughts on enum constants
      2. Accessing classes
      3. Walking methods
    4. StackFrame
    5. Performance
    6. Summary
  12. New Tools and Tool Enhancements
    1. The new HTTP client [JEP-110]
      1. The HTTP client before Java 9
      2. Java 9's new HTTP client
      3. New API limitations
    2. Simplified Doclet API [JEP-221]
      1. The pre-Java 9 Doclet API
        1. API enums
        2. API classes
        3. API interfaces
        4. Problems with the pre-existing Doclet API
      2. 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
    3. HTML5 Javadoc [JEP-224]
    4. Javadoc search [JEP-225]
      1. Introducing camel case search
    5. Remove launch-time JRE version selection [JEP-231]
    6. Parser API for Nashorn [JEP-236]
      1. Nashorn
        1. Using Nashorn as a command-line tool
        2. Using Nashorn as an embedded interpreter
      2. EMCAScript
      3. Parser API
    7. Multi-release JAR files [JEP-238]
      1. Identifying multi-release JAR files
      2. Related JDK changes
    8. Java-level JVM compiler interface [JEP-243]
    9. BeanInfo annotations [JEP-256]
      1. JavaBean
      2. BeanProperty
      3. SwingContainer
      4. BeanInfo classes
    10. TIFF image input/output [JEP-262]
    11. Platform logging API and service [JEP-264]
      1. The java.util.logging package
      2. Logging in Java 9
    12. XML Catalogs [JEP-268]
      1. The OASIS XML Catalog standard
      2. JAXP processors
      3. XML Catalogs prior to Java 9
      4. Java 9 platform changes
    13. Convenience factory methods for collections [JEP-269]
      1. Using collections before Java 9
      2. Using new collection literals
    14. Platform-specific desktop features [JEP-272]
    15. Enhanced method handles [JEP-274]
      1. Reason for the enhancement
      2. Lookup functions
      3. Argument handling
      4. Additional combinations
    16. Enhanced deprecation [JEP-277]
      1. What the @Deprecated annotation really means
    17. Summary
  13. Concurrency and Reactive Programming
    1. Reactive Programming
      1. Reactive programming standardization
    2. The New 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
    3. Additional Concurrency Updates
      1. Java concurrency
        1. Concurrency explained
        2. System configurations
        3. Java threads
        4. Concurrency improvements
      2. CompletableFuture API enhancements
        1. Class details
        2. Enhancements
    4. Spin-Wait Hints
    5. Summary
  14. Security Enhancements
    1. Datagram Transport Layer Security
      1. DTLS protocol version 1.0
      2. DTLS protocol version 1.2
      3. DTLS support in Java 9
    2. Creating PKCS12 keystores
      1. Keystore primer
      2. Java Keystore (JKS)
        1. 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
    3. Improving security application performance
      1. Security policy enforcement
      2. Permission evaluation
      3. The java.Security.CodeSource package
      4. Package checking algorithm
    4. TLS application-layer protocol negotiation extension
      1. TLS ALPN extension
      2. The javax.net.ssl package
      3. The java.net.ssl package extension
    5. Leveraging CPU instructions for GHASH and RSA
      1. Hashing
    6. OCSP stapling for TLS
      1. OCSP stapling primer
      2. Changes for the Java 9 platform
    7. DRBG-based SecureRandom implementations
    8. Summary
  15. Command Line Flags
    1. Unified JVM Logging [JEP 158]
      1. Command-line options
      2. Decorations
      3. Levels
      4. Output
      5. Tags
    2. Compiler control [JEP 165]
      1. Compilation modes
        1. C1 compilation mode
        2. C2 compilation mode
        3. Tiered compilation
      2. Compiler control in Java 9
    3. Diagnostic commands [JEP 228]
    4. Heap profiling agent [JEP 240]
    5. Removing your JHAT [JEP 241]
    6. JVM command-line flag argument validation [JEP 245]
    7. Compile for older platform versions [JEP 247]
    8. Summary
  16. Best Practices In Java 9
    1. Support for UTF-8
      1. The ResourceBundle class
        1. The nested class
        2. Fields and constructors
        3. Methods
      2. Changes in Java 9
    2. Unicode 7.0.0
      1. The java.lang package
      2. The java.text package
      3. Additional significance
    3. The Linux/AArch64 port
    4. Multi-resolution Images
    5. Common Locale Data Repository (CLDR)
    6. Summary
  17. Future Directions
    1. Future Changes to the JDK
      1. JDK changes targeted for Java 10
        1. Repository consolidation
        2. Native-header tool removal
      2. JDK-related submitted proposals
        1. Parallelize the Full GC Phase in CMS
        2. REST APIs for JMX
        3. Support heap allocation
      3. JDK-related drafted proposals
        1. Finalization promptness
        2. Java memory model
        3. Foreign Function Interfaces
        4. Isolated methods
        5. Reducing metaspace waste
        6. Improving IPv6 support
        7. Unboxed argument lists for method handles
        8. Enhanced MandelblotSet demo using value types
        9. Efficient array comparison intrinsics
    2. Future changes to the Java Compiler
      1. Policy for retiring javac -source and -target options
      2. Pluggable static analyzers
    3. Future Changes to the Java Virtual Machine
      1. JVM-related submitted proposals
        1. Container aware Java
        2. Enable execution of Java methods on GPU
        3. Epsilon GC - The arbitrarily low overhead garbage (non-) collector
      2. JVM-related drafted proposals
        1. Provide stable USDT probe points on JVM compiled methods
        2. Concurrent monitor deflation
        3. Provide a low-overhead way of sampling Java heap allocations
        4. Diagnostic Command Framework
        5. Enhanced Class Redefinition
        6. Enable NUMA mode by default when appropriate
        7. Value objects
        8. Align JVM Access Checks
    4. Future Changes to JavaX
      1. JMX specific annotations for registration of managed resources
      2. Modernize the GTK3 Look and Feel Implementation
    5. Ongoing Special Projects
      1. Annotations pipeline 2.0
      2. Audio Synthesis Engine
      3. Caciocavallo
      4. Common VM Interface
      5. Compiler Grammar
      6. Da Vinci Machine
      7. Device I/O
      8. Graal
      9. HarfBuzz Integration
      10. Kona
      11. OpenJFX
      12. Panama
      13. Shenandoah
    6. Summary