About This eBook
ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the eBook in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Java SE 8 for the Really Impatient
Cay S. Horstmann
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
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 the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the United States, please contact .
Visit us on the Web: informit.com/aw
Cataloging-in-Publication Data is on file with the Library of Congress.
Copyright 2014 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-92776-7
ISBN-10: 0-321-92776-1
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, January 2014
To Greg Doench, my editor for two decades, whose patience, kindness, and good judgment I greatly admire
Preface
This book gives a concise introduction to the many new features of Java 8 (and a few features of Java 7 that havent received much attention) for programmers who are already familiar with Java.
This book is written in the impatient style that I first tried out in a book called Scala for the Impatient. In that book, I wanted to quickly cut to the chase without lecturing the reader about the superiority of one paradigm over another. I presented information in small chunks organized to help you quickly retrieve it when needed. The approach was a big success in the Scala community, and I am employing it again in this book.
With Java 8, the Java programming language and library receive a major refresh. Lambda expressions make it possible to write snippets of computations in a concise way, so that you can pass them to other code. The recipient can choose to execute your computation when appropriate and as often as appropriate. This has a profound impact on building libraries.
In particular, working with collections has completely changed. Instead of specifying how to compute a result (traverse from the beginning to the end, and if an element matches a condition, compute a value from it, and add that value to a sum), you specify what you want (give me the sum of all elements that match a condition). The library is then able to reorder the computationfor example, to take advantage of parallelism. Or, if you just want to have the first hundred matches, it can stop the computation without you having to maintain a counter.
, I provide you with tips on how to effectively design your own libraries with lambdas.
With Java 8, developers of client-side applications need to transition to the JavaFX API since Swing is now in maintenance mode. gives a quick introduction to JavaFX for a programmer who needs to put together a graphical programwhen a picture speaks louder than 1,000 strings.
Having waited for far too many years, programmers are finally able to use a well-designed date/time library. covers the java.time
API in detail.
Each version of Java brings enhancements in the concurrency API, and Java 8 is no exception. In , you learn about improvements in atomic counters, concurrent hash maps, parallel array operations, and composable futures.
Java 8 bundles Nashorn, a high-quality JavaScript implementation. In , you will see how to execute JavaScript on the Java Virtual Machine, and how to interoperate with Java code.
does the same for Java 7, focusing on improved exception handling, the new I/O enhancements for working with files and directories, and other library enhancements that you may have missed.
My thanks go, as always, to my editor Greg Doench, who had the idea of a short book that brings experienced programmers up to speed with Java 8. Dmitry Kirsanov and Alina Kirsanova once again turned an XHTML manuscript into an attractive book with amazing speed and attention to detail. I am grateful to the reviewers who spotted many embarrassing errors and gave excellent suggestions for improvement. They are: Gail Anderson, Paul Anderson, James Denvir, Trisha Gee, Brian Goetz (special thanks for the very thorough review), Marty Hall, Angelika Langer, Mark Lawrence, Stuart Marks, Attila Szegedi, and Jim Weaver.
I hope that you enjoy reading this concise introduction to the new features of Java 8, and that it will make you a more successful Java programmer. If you find errors or have suggestions for improvement, please visit http://horstmann.com/java8 and leave a comment. On that page, you will also find a link to an archive file containing all code examples from the book.
Cay Horstmann
San Francisco, 2013
About the Author
Cay S. Horstmann is the author of Scala for the Impatient (Addison-Wesley, 2012), is principal author of Core Java, Volumes I and II, Ninth Edition (Prentice Hall, 2013), and has written a dozen other books for professional programmers and computer science students. He is a professor of computer science at San Jose State University and is a Java Champion.