6 t h C E o Java in a Nutshell vers diti J o SIXTH av n The latest edition of Java in a Nutshell is designed to “ In a world of blogged EDITION a 8 help experienced Java programmers get the most out opinions and javadoc’d J of Java 7 and 8, but it’s also a learning path for new references, this latest developers. Chock full of examples that demonstrate a how to take complete advantage of modern Java APIs edition is still the v and development best practices, the first section of simplest and most this thoroughly updated book provides a fast-paced, definitive way to cut a no-fluff introduction to the Java programming language through to the answers and the core runtime aspects of the Java platform. you need.” i The second section is a reference to core concepts n and APIs that shows you how to perform real —Kevlin Henney programming work in the Java environment. consultant, author, speaker, editor of 97 Things Every a Programmer Should Know ■ Get up to speed on language details, including Java 8 changes N ■ Learn object-oriented programming, using basic Java syntax u Benjamin J. Evans is the ■ Explore generics, enumerations, cofounder and Technology annotations, and lambda expressions Fellow of jClarity, a startup that t J J aavvaa delivers performance tools to help s ■ Understand basic techniques used in development & ops teams. He is a object-oriented design Java Champion; JavaOne Rockstar; h coauthor of The Well-Grounded ■ Examine concurrency and memory, and Java Developer (Manning); and e how they’re intertwined a regular public speaker on the ■ Work with Java collections and handle Java platform, performance, l concurrency, and related topics. l common data formats David Flanagan, senior staff ■ Delve into Java’s latest I/O APIs, including frontend software engineer at asynchronous channels iinn aa NNuuttsshheellll Mozilla, has written several books ■ Use Nashorn to execute JavaScript on the for O’Reilly, including JavaScript: The Definitive Guide, jQuery Pocket Java Virtual Machine Reference, The Ruby Programming F ■ Become familiar with development tools in Language, and previous editions of laEv n OpenJDK Java in a Nutshell. aan g s an & A DESKTOP QUICK REFERENCE PROGRAMMING/JAVA Twitter: @oreillymedia facebook.com/oreilly US $59.99 CAN $62.99 ISBN: 978-1-449-37082-4 Benjamin J. Evans & David Flanagan 6 t h C E o Java in a Nutshell vers diti J o SIXTH av n The latest edition of Java in a Nutshell is designed to “ In a world of blogged EDITION a 8 help experienced Java programmers get the most out opinions and javadoc’d J of Java 7 and 8, but it’s also a learning path for new references, this latest developers. Chock full of examples that demonstrate a how to take complete advantage of modern Java APIs edition is still the v and development best practices, the first section of simplest and most this thoroughly updated book provides a fast-paced, definitive way to cut a no-fluff introduction to the Java programming language through to the answers and the core runtime aspects of the Java platform. you need.” i The second section is a reference to core concepts n and APIs that shows you how to perform real —Kevlin Henney programming work in the Java environment. consultant, author, speaker, editor of 97 Things Every a Programmer Should Know ■ Get up to speed on language details, including Java 8 changes N ■ Learn object-oriented programming, using basic Java syntax u Benjamin J. Evans is the ■ Explore generics, enumerations, cofounder and Technology annotations, and lambda expressions Fellow of jClarity, a startup that t J J aavvaa delivers performance tools to help s ■ Understand basic techniques used in development & ops teams. He is a object-oriented design Java Champion; JavaOne Rockstar; h coauthor of The Well-Grounded ■ Examine concurrency and memory, and Java Developer (Manning); and e how they’re intertwined a regular public speaker on the ■ Work with Java collections and handle Java platform, performance, l concurrency, and related topics. l common data formats David Flanagan, senior staff ■ Delve into Java’s latest I/O APIs, including frontend software engineer at asynchronous channels iinn aa NNuuttsshheellll Mozilla, has written several books ■ Use Nashorn to execute JavaScript on the for O’Reilly, including JavaScript: The Definitive Guide, jQuery Pocket Java Virtual Machine Reference, The Ruby Programming F ■ Become familiar with development tools in Language, and previous editions of laEv n OpenJDK Java in a Nutshell. aan g s an & A DESKTOP QUICK REFERENCE PROGRAMMING/JAVA Twitter: @oreillymedia facebook.com/oreilly US $59.99 CAN $62.99 ISBN: 978-1-449-37082-4 Benjamin J. Evans & David Flanagan JAVA IN A NUTSHELL Sixth Edition Benjamin J. Evans and David Flanagan Java in a Nutshell by Benjamin J. Evans and David Flanagan Copyright © 2015 Benjamin J. Evans and David Flanagan. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://www.safaribooksonline.com). For more infor‐ mation, contact our corporate/institutional sales department: 800-998-9938 or corpo‐ [email protected]. Editors: Mike Loukides and Indexer: Ellen Troutman Zaig Meghan Blanchette Interior Designer: David Futato Production Editor: Matthew Hacker Cover Designer: Ellie Volckhausen Copyeditor: Charles Roumeliotis Illustrator: Rebecca Demarest Proofreader: Jasmine Kwityn February 1996: First Edition March 2002: Fourth Edition May 1997: Second Edition March 2005: Fifth Edition November 1999: Third Edition October 2014: Sixth Edition Revision History for the Sixth Edition 2014-10-10: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781449370824 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trade‐ marks of O’Reilly Media, Inc. Java in a Nutshell, the cover image of a Javan tiger, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While the publisher and the authors have used good faith efforts to ensure that the informa‐ tion and instructions contained in this work are accurate, the publisher and the authors dis‐ claim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technol‐ ogy this work contains or describes is subject to open source licenses or the intellectual prop‐ erty rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-449-37082-4 [LSI] This book is dedicated to all who teach peace and resist violence. Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I. Introducing Java 1. Introduction to the Java Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Language, the JVM, and the Ecosystem 3 A Brief History of Java and the JVM 7 The Lifecycle of a Java Program 9 Java Security 11 Comparing Java to Other Languages 11 Answering Some Criticisms of Java 13 2. Java Syntax from the Ground Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Java Programs from the Top Down 18 Lexical Structure 18 Primitive Data Types 22 Expressions and Operators 30 Statements 46 Methods 66 Introduction to Classes and Objects 72 Arrays 77 Reference Types 84 Packages and the Java Namespace 88 Java File Structure 93 Defining and Running Java Programs 94 Summary 95 vii 3. Object-Oriented Programming in Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Overview of Classes 97 Fields and Methods 100 Creating and Initializing Objects 106 Subclasses and Inheritance 110 Data Hiding and Encapsulation 121 Abstract Classes and Methods 128 Modifier Summary 132 4. The Java Type System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Interfaces 136 Java Generics 142 Enums and Annotations 151 Nested Types 155 Lambda Expressions 171 Conclusion 174 5. Introduction to Object-Oriented Design in Java. . . . . . . . . . . . . . . . . . . . . . . 177 Java Values 177 Important Methods of java.lang.Object 178 Aspects of Object-Oriented Design 183 Exceptions and Exception Handling 193 Safe Java Programming 195 6. Java’s Approach to Memory and Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . 197 Basic Concepts of Java Memory Management 197 How the JVM Optimizes Garbage Collection 201 The HotSpot Heap 203 Finalization 206 Java’s Support for Concurrency 208 Working with Threads 218 Summary 219 Part II. Working with the Java Platform 7. Programming and Documentation Conventions. . . . . . . . . . . . . . . . . . . . . . . 223 Naming and Capitalization Conventions 223 Practical Naming 225 Java Documentation Comments 226 Conventions for Portable Programs 235 viii | Table of Contents 8. Working with Java Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Introduction to Collections API 239 Lambda Expressions in the Java Collections 258 Conclusion 266 9. Handling Common Data Formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Text 267 Numbers and Math 275 Java 8 Date and Time 280 Conclusion 287 10. File Handling and I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Classic Java I/O 289 Modern Java I/O 295 NIO Channels and Buffers 298 Async I/O 301 Networking 304 11. Classloading, Reflection, and Method Handles. . . . . . . . . . . . . . . . . . . . . . . . 311 Class Files, Class Objects, and Metadata 311 Phases of Classloading 313 Secure Programming and Classloading 315 Applied Classloading 317 Reflection 320 Dynamic Proxies 325 Method Handles 326 12. Nashorn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Introduction to Nashorn 331 Executing JavaScript with Nashorn 332 Nashorn and javax.script 340 Advanced Nashorn 342 Conclusion 347 13. Platform Tools and Profiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Command-Line Tools 349 VisualVM 362 Java 8 Profiles 367 Conclusion 372 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Table of Contents | ix