This book is a desktop Java reference, designed to sit faithfully by your keyboard while you program. Part I of the book 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 8, 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 (and sometimes Java 7). We use Java 8 syntax throughout, including using lambda expressions in code that would previously have used a trivial anonymous nested class.
The fifth edition of this book covers Java 5, whereas this edition covers Java 8. The language, and the working environment of the programmer, have both changed considerably since the last edition was published nearly a decade ago. This new edition has, accordingly, changed a vast amount as well. One very important aspect is that this book does not attempt to be as complete a description of the core platform APIs as was possible in earlier editions.
For one thing, the sheer size of the core APIs render this utterly impractical for a printed book. A more compelling reason is the continued rise of fast, always-on Internet. The amount of Java programmers who regularly work without Internet access is now vanishingly small. The proper place for detailed reference API docs is online, not printed out.
Accordingly, the reference section, which occupied two-thirds of the fifth edition, is gone. In the space we’ve recovered, 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 gained in importance—even among mainstream developers.
In this edition, we have tried to reflect this changed world, and have largely abandoned the historical approach of earlier editions. In particular, the exhaustive attempt to detail exactly which version of Java particular features arrived with has mostly been abandoned—only the most recent versions of Java are likely to be of interest to the majority of Java developers.
The first six chapters of this book 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 a second 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 OO 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 is 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. The second part of the book 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. Part II 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 is 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 Nashorn, an implementation of JavaScript running atop the Java Virtual Machine. Nashorn ships with Java 8, and provides an alternative to other JavaScript implementations. Toward the end of the chapter, we discuss Avatar.js—a server-side technology compatible with Node.
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. The second part of the chapter covers Compact Profiles—a new feature in Java 8 allowing cut-down Java Runtime Environments (JREs) with a significantly reduced footprint.
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 home page for the book at http://www.oreilly.com/catalog/javanut6. 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 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. Sometimes 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 [email protected].
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_6e.
To comment or ask technical questions about this book, send email to [email protected].
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 Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.
Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, 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, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.
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.