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.
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.
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:
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.
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).
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.
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.
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.
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:
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.
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.
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.
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.
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.
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.
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.
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.
O’Reilly publishes an entire series of books on Java programming, including several companion books to this one. The companion books are:
This book is a comprehensive tutorial introduction to Java, and includes topics such as XML and client-side Java programming.
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.
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/.
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.
We use the following formatting conventions in this book:
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 widthUsed 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 italicUsed 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.
This element signifies a tip or suggestion.
This element signifies a general note.
This element indicates a warning or caution.
You can send comments, fixes, and suggestions directly to the authors by using the email address javanut7@gmail.com.
Please address comments and questions concerning this book to the publisher:
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 bookquestions@oreilly.com.
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
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.
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.