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

Java in a Nutshell, 7th Edition

Book Description

This updated edition of Java in a Nutshell not only helps experienced Java programmers get the most out of Java versions 9 through 11, it’s also a learning path for new developers. Chock full of examples that demonstrate how to take complete advantage of modern Java APIs and development best practices, this thoroughly revised book includes new material on Java Concurrency Utilities.

The book’s first section provides a fast-paced, no-fluff introduction to the Java programming language and the core runtime aspects of the Java platform. The second section is a reference to core concepts and APIs that explains how to perform real programming work in the Java environment.

  • Get up to speed on language details, including Java 9-11 changes
  • Learn object-oriented programming, using basic Java syntax
  • Explore generics, enumerations, annotations, and lambda expressions
  • Understand basic techniques used in object-oriented design
  • Examine concurrency and memory, and how they’re intertwined
  • Work with Java collections and handle common data formats
  • Delve into Java’s latest I/O APIs, including asynchronous channels
  • Use Nashorn to execute JavaScript on the Java Virtual Machine
  • Become familiar with development tools in OpenJDK

Table of Contents

  1. Foreword
  2. Preface
    1. Changes in the Seventh Edition
    2. Contents of This Book
    3. Related Books
    4. Examples Online
    5. Conventions Used in This Book
    6. Request for Comments
    7. O’Reilly Safari
    8. Acknowledgments
  3. I. Introducing Java
  4. 1. Introduction to the Java Environment
    1. The Language, the JVM, and the Ecosystem
      1. What Is the Java Language?
      2. What Is the JVM?
      3. What Is the Java Ecosystem?
    2. A Brief History of Java and the JVM
    3. The Lifecycle of a Java Program
      1. Frequently Asked Questions
    4. Java Security
    5. Comparing Java to Other Languages
      1. Java Compared to C
      2. Java Compared to C++
      3. Java Compared to Python
      4. Java Compared to JavaScript
    6. Answering Some Criticisms of Java
      1. Overly Verbose
      2. Slow to Change
      3. Performance Problems
      4. Insecure
      5. Too Corporate
  5. 2. Java Syntax from the Ground Up
    1. Java Programs from the Top Down
    2. Lexical Structure
      1. The Unicode Character Set
      2. Case Sensitivity and Whitespace
      3. Comments
      4. Reserved Words
      5. Identifiers
      6. Literals
      7. Punctuation
    3. Primitive Data Types
      1. The boolean Type
      2. The char Type
      3. Integer Types
      4. Floating-Point Types
      5. Primitive Type Conversions
    4. Expressions and Operators
      1. Operator Summary
      2. Arithmetic Operators
      3. String Concatenation Operator
      4. Increment and Decrement Operators
      5. Comparison Operators
      6. Boolean Operators
      7. Bitwise and Shift Operators
      8. Assignment Operators
      9. The Conditional Operator
      10. The instanceof Operator
      11. Special Operators
    5. Statements
      1. Expression Statements
      2. Compound Statements
      3. The Empty Statement
      4. Labeled Statements
      5. Local Variable Declaration Statements
      6. The if/else Statement
      7. The switch Statement
      8. The while Statement
      9. The do Statement
      10. The for Statement
      11. The foreach Statement
      12. The break Statement
      13. The continue Statement
      14. The return Statement
      15. The synchronized Statement
      16. The throw Statement
      17. The try/catch/finally Statement
      18. The try-with-resources Statement
      19. The assert Statement
    6. Methods
      1. Defining Methods
      2. Method Modifiers
      3. Checked and Unchecked Exceptions
      4. Variable-Length Argument Lists
    7. Introduction to Classes and Objects
      1. Defining a Class
      2. Creating an Object
      3. Using an Object
      4. Object Literals
      5. Lambda Expressions
    8. Arrays
      1. Array Types
      2. Creating and Initializing Arrays
      3. Using Arrays
      4. Multidimensional Arrays
    9. Reference Types
      1. Reference Versus Primitive Types
      2. Manipulating Objects and Reference Copies
      3. Comparing Objects
      4. Boxing and Unboxing Conversions
    10. Packages and the Java Namespace
      1. Package Declaration
      2. Globally Unique Package Names
      3. Importing Types
      4. Importing Static Members
    11. Java Source File Structure
    12. Defining and Running Java Programs
    13. Summary
  6. 3. Object-Oriented Programming in Java
    1. Overview of Classes
      1. Basic OO Definitions
      2. Other Reference Types
      3. Class Definition Syntax
    2. Fields and Methods
      1. Field Declaration Syntax
      2. Class Fields
      3. Class Methods
      4. Instance Fields
      5. Instance Methods
      6. How the this Reference Works
    3. Creating and Initializing Objects
      1. Defining a Constructor
      2. Defining Multiple Constructors
      3. Invoking One Constructor from Another
      4. Field Defaults and Initializers
    4. Subclasses and Inheritance
      1. Extending a Class
      2. Superclasses, Object, and the Class Hierarchy
      3. Subclass Constructors
      4. Constructor Chaining and the Default Constructor
      5. Hiding Superclass Fields
      6. Overriding Superclass Methods
    5. Data Hiding and Encapsulation
      1. Access Control
      2. Data Accessor Methods
    6. Abstract Classes and Methods
      1. Reference Type Conversions
    7. Modifier Summary
  7. 4. The Java Type System
    1. Interfaces
      1. Defining an Interface
      2. Extending Interfaces
      3. Implementing an Interface
      4. Default Methods
      5. Marker Interfaces
    2. Java Generics
      1. Introduction to Generics
      2. Generic Types and Type Parameters
      3. Diamond Syntax
      4. Type Erasure
      5. Bounded Type Parameters
      6. Introducing Covariance
      7. Wildcards
      8. Generic Methods
      9. Compile and Runtime Typing
      10. Using and Designing Generic Types
    3. Enums and Annotations
      1. Enums
      2. Annotations
      3. Defining Custom Annotations
      4. Type Annotations
    4. Lambda Expressions
      1. Lambda Expression Conversion
      2. Method References
      3. Functional Programming
      4. Lexical Scoping and Local Variables
    5. Nested Types
      1. Static Member Types
      2. Nonstatic Member Classes
      3. Local Classes
      4. Anonymous Classes
    6. Non-Denotable Types and var
    7. Summary
  8. 5. Introduction to Object-Oriented Design in Java
    1. Java Values
    2. Important Methods of java.lang.Object
      1. toString()
      2. equals()
      3. hashCode()
      4. Comparable::compareTo()
      5. clone()
    3. Aspects of Object-Oriented Design
      1. Constants
      2. Interfaces Versus Abstract Classes
      3. Can Default Methods Be Used as Traits?
      4. Instance Methods or Class Methods?
      5. Composition Versus Inheritance
      6. Field Inheritance and Accessors
      7. Singleton
    4. Object-Oriented Design with Lambdas
      1. Lambdas Versus Nested Classes
      2. Lambdas Versus Method References
    5. Exceptions and Exception Handling
    6. Safe Java Programming
  9. 6. Java’s Approach to Memory and Concurrency
    1. Basic Concepts of Java Memory Management
      1. Memory Leaks in Java
      2. Introducing Mark-and-Sweep
      3. The Basic Mark-and-Sweep Algorithm
    2. How the JVM Optimizes Garbage Collection
      1. Evacuation
    3. The HotSpot Heap
      1. Other Collectors
      2. ParallelOld
    4. Finalization
      1. Finalization Details
    5. Java’s Support for Concurrency
      1. Thread Lifecycle
      2. Visibility and Mutability
      3. Exclusion and Protecting State
      4. volatile
      5. Useful Methods of Thread
      6. Deprecated Methods of Thread
    6. Working with Threads
    7. Summary
  10. II. Working with the Java Platform
  11. 7. Programming and Documentation Conventions
    1. Naming and Capitalization Conventions
    2. Practical Naming
    3. Java Documentation Comments
      1. Structure of a Doc Comment
      2. Doc-Comment Tags
      3. Inline Doc-Comment Tags
      4. Cross-References in Doc Comments
      5. Doc Comments for Packages
    4. Doclets
    5. Conventions for Portable Programs
  12. 8. Working with Java Collections
    1. Introduction to Collections API
      1. The Collection Interface
      2. The Set Interface
      3. The List Interface
      4. The Map Interface
      5. The Queue and BlockingQueue Interfaces
      6. Adding Elements to Queues
      7. Removing Elements from Queues
      8. Utility Methods
      9. Arrays and Helper Methods
    2. Java Streams and Lambda Expressions
      1. Functional Approaches
      2. The Streams API
    3. Summary
  13. 9. Handling Common Data Formats
    1. Text
      1. Special Syntax for Strings
      2. String Immutability
      3. Regular Expressions
    2. Numbers and Math
      1. How Java Represents Integer Types
      2. Java and Floating-Point Numbers
      3. Java’s Standard Library of Mathematical Functions
    3. Java 8 Date and Time
      1. Introducing the Java 8 Date and Time API
      2. Queries
      3. Adjusters
      4. Legacy Date and Time
    4. Summary
  14. 10. File Handling and I/O
    1. Classic Java I/O
      1. Files
      2. Streams
      3. Readers and Writers
      4. try-with-resources Revisited
      5. Problems with Classic I/O
    2. Modern Java I/O
      1. Files
      2. Path
    3. NIO Channels and Buffers
      1. ByteBuffer
      2. Mapped Byte Buffers
    4. Async I/O
      1. Future-Based Style
      2. Callback-Based Style
      3. Watch Services and Directory Searching
    5. Networking
      1. HTTP
      2. TCP
      3. IP
  15. 11. Classloading, Reflection, and Method Handles
    1. Class Files, Class Objects, and Metadata
      1. Examples of Class Objects
      2. Class Objects and Metadata
    2. Phases of Classloading
      1. Loading
      2. Verification
      3. Preparation and Resolution
      4. Initialization
    3. Secure Programming and Classloading
    4. Applied Classloading
      1. Classloader Hierarchy
    5. Reflection
      1. When to Use Reflection
      2. How to Use Reflection
      3. Dynamic Proxies
    6. Method Handles
      1. MethodType
      2. Method Lookup
      3. Invoking Method Handles
  16. 12. Java Platform Modules
    1. Why Modules?
      1. Modularizing the JDK
    2. Writing Your Own Modules
      1. Basic Modules Syntax
      2. Building a Simple Modular Application
      3. The Module Path
      4. Automatic Modules
      5. Open Modules
      6. Services
      7. Multi-Release JARs
      8. Converting to a Multi-Release JAR
      9. Migrating to Modules
      10. Custom Runtime Images
    3. Issues with Modules
      1. Unsafe and Related Problems
      2. Lack of Versioning
      3. Slow Adoption Rates
    4. Summary
  17. 13. Platform Tools
    1. Command-Line Tools
    2. Introduction to JShell
    3. Summary
  18. A. Additional Tools
    1. Introduction to Nashorn
      1. Non-Java Languages on the JVM
      2. Motivation
    2. Executing JavaScript with Nashorn
      1. Running from the Command Line
      2. Using the Nashorn Shell
    3. Nashorn and javax.script
      1. Introducing javax.script with Nashorn
    4. Advanced Nashorn
      1. Calling Java from Nashorn
      2. Nashorn’s JavaScript Language Extensions
      3. Under the Hood
    5. The Future of Nashorn and GraalVM
    6. VisualVM
  19. Index