Preface

This book is a desktop Java reference, designed to sit faithfully by your keyboard while you program. Part I is a fast-paced, “no-fluff” introduction to the Java programming language and the core runtime aspects of the Java platform. Part II is a reference section that blends elucidation of core concepts with examples of important core APIs. The book covers Java 11, but we recognize that some shops may not have adopted it yet—so where possible we call out if a feature was introduced in Java 8, 9, or 10. We use Java 11 syntax throughout, including var and lambda expressions.

Changes in the Seventh Edition

The sixth edition of this book covers Java 8, whereas this edition covers Java 11. However, the release process of Java changed significantly with the advent of Java 9, so this book is released only a year after Java 9 arrived. Java 11 is also the first long-term support (LTS) release of Java since Java 8, so it seems likely that many Java shops will jump straight to Java 11 from Java 8.

With the seventh edition we have tried to update the concept of what it means to be a “Nutshell” guide. The modern Java developer needs to know more than just syntax and APIs. As the Java environment has matured, such topics as concurrency, object-oriented design, memory, and the Java type system have all grown in importance—even among mainstream developers.

In this edition, we have taken the approach that only the most recent versions of Java are likely to be of interest to the majority of Java developers, so we usually only call out when new features arrived if it was with Java 8 or later.

The module system (that arrived with Java 9) is still likely to be new for at least some developers, and it represents a major change.

Contents of This Book

The first six chapters document the Java language and the Java platform—they should all be considered essential reading. The book is biased toward the Oracle/OpenJDK (Open Java Development Kit) implementation of Java, but not greatly so. Developers working with other Java environments will still find plenty to occupy them. Part I includes:

Chapter 1, Introduction to the Java Environment

This chapter is an overview of the Java language and the Java platform. It explains the important features and benefits of Java, including the lifecycle of a Java program. We also touch on Java security and answer some criticisms of Java.

Chapter 2, Java Syntax from the Ground Up

This chapter explains the details of the Java programming language, including the Java 8 language changes. It is a long and detailed chapter that does not assume substantial programming experience. Experienced Java programmers can use it as a language reference. Programmers with substantial experience with languages such as C and C++ should be able to pick up Java syntax quickly by reading this chapter; beginning programmers with only a modest amount of experience should be able to learn Java programming by studying this chapter carefully, although it is best read in conjunction with an introductory text (such as O’Reilly’s Head First Java by Bert Bates and Kathy Sierra).

Chapter 3, Object-Oriented Programming in Java

This chapter describes how the basic Java syntax documented in Chapter 2 is used to write simple object-oriented programs using classes and objects in Java. The chapter assumes no prior experience with object-oriented programming. It can be used as a tutorial by new programmers or as a reference by experienced Java programmers.

Chapter 4, The Java Type System

This chapter builds on the basic description of object-oriented programming in Java, and introduces the other aspects of Java’s type system, such as generic types, enumerated types, and annotations. With this more complete picture, we can discuss the biggest change in Java 8—the arrival of lambda expressions.

Chapter 5, Introduction to Object-Oriented Design in Java

This chapter is an overview of some basic techniques used in the design of sound object-oriented programs, and briefly touches on the topic of design patterns and their use in software engineering.

Chapter 6, Java’s Approach to Memory and Concurrency

This chapter explains how the Java Virtual Machine manages memory on behalf of the programmer, and how memory and visibility are intimately entwined with Java’s support for concurrent programming and threads.

These first six chapters teach you the Java language and get you up and running with the most important concepts of the Java platform. Part II is all about how to get real programming work done in the Java environment. It contains plenty of examples and is designed to complement the cookbook approach found in some other texts. This part includes:

Chapter 7, Programming and Documentation Conventions

This chapter documents important and widely adopted Java programming conventions. It also explains how you can make your Java code self-documenting by including specially formatted documentation comments.

Chapter 8, Working with Java Collections

This chapter introduces Java’s standard collections libraries. These contain data structures that are vital to the functioning of virtually every Java program—such as List, Map, and Set. The new Stream abstraction and the relationship between lambda expressions and the collections are explained in detail.

Chapter 9, Handling Common Data Formats

This chapter discusses how to use Java to work effectively with very common data formats, such as text, numbers, and temporal (date and time) information.

Chapter 10, File Handling and I/O

This chapter covers several different approaches to file access—from the more classic approach found in older versions of Java, through to more modern and even asynchronous styles. The chapter concludes with a short introduction to networking with the core Java platform APIs.

Chapter 11, Classloading, Reflection, and Method Handles

This chapter introduces the subtle art of metaprogramming in Java—first introducing the concept of metadata about Java types, then turning to the subject of classloading and how Java’s security model is linked to the dynamic loading of types. The chapter concludes with some applications of classloading and the relatively new feature of method handles.

Chapter 12, Java Platform Modules

This chapter describes Java Platform Modules (JPMS), the major feature that was introduced as part of Java 9, and provides an introduction to the wide-ranging changes that it brings.

Chapter 13, Platform Tools

Oracle’s JDK (as well as OpenJDK) includes a number of useful Java development tools, most notably the Java interpreter and the Java compiler. This chapter documents those tools, as well as the jshell interactive environment and new tools for working with modular Java.

Appendix A, Additional Tools

This appendix covers Nashorn, an implementation of JavaScript running atop the Java Virtual Machine. Nashorn ships with Java 8, and provides an alternative to other JavaScript implementations.

Related Books

O’Reilly publishes an entire series of books on Java programming, including several companion books to this one. The companion books are:

Learning Java by Patrick Niemeyer and Daniel Leuck

This book is a comprehensive tutorial introduction to Java, and includes topics such as XML and client-side Java programming.

Java 8 Lambdas by Richard Warburton

This book documents the new Java 8 feature of lambda expressions in detail, and introduces concepts of functional programming that may be unfamiliar to Java developers coming from earlier versions.

Head First Java by Bert Bates and Kathy Sierra

This book uses a unique approach to teaching Java. Developers who think visually often find it a great accompaniment to a traditional Java book.

You can find a complete list of Java books from O’Reilly at http://java.oreilly.com/.

Examples Online

The examples in this book are available online and can be downloaded from the book’s website. You may also want to visit this site for any important notes or errata that have been published there.

Conventions Used in This Book

We use the following formatting conventions in this book:

Italic

Used for emphasis and to signify the first use of a term. Italic is also used for commands, email addresses, websites, FTP sites, and file and directory names.

Constant width

Used for all Java code as well as for anything that you would type literally when programming, including keywords, data types, constants, method names, variables, class names, and interface names.

Constant width italic

Used for the names of function arguments and generally as a placeholder to indicate an item that should be replaced with an actual value in your program. Also used to refer to a conceptual section or line of code as in statement.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Request for Comments

You can send comments, fixes, and suggestions directly to the authors by using the email address .

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/java_nutshell_7e.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.

Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.

For more information, please visit http://oreilly.com/safari.

Acknowledgments

Meghan Blanchette was the editor of the sixth edition—her attention to detail and cheerful, grounded approach helped provide extra momentum at very useful moments throughout the book’s development.

Special thanks are due to Jim Gough, Richard Warburton, John Oliver, Trisha Gee, and Stephen Colebourne.

As always, Martijn Verburg has been a good friend, business partner, sounding board, and font of useful advice.

Ben, in particular, would like to thank everyone who has given him feedback and helped him improve as a writer. Caroline Kvitka, Victor Grazi, Tori Weildt, and Simon Ritter deserve special mention for their helpful suggestions. If he’s failed to take all of their excellent advice in this text the blame is, of course, his.