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.
Core Java for the 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
Library of Congress Cataloging-in-Publication Data
Horstmann, Cay S., 1959
Core Java for the impatient / Cay S. Horstmann.
pages cm
Includes index.
ISBN 978-0-321-99632-9 (pbk. : alk. paper)ISBN 0-321-99632-1 (pbk. : alk. paper)
1. Java (Computer program language) I. Title.
QA76.73.J38H67535 2015
005.133dc23
2014046523
Copyright 2015 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-99632-9
ISBN-10: 0-321-99632-1
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing, February 2015
To Chithe most patient person in my life.
Contents
Preface
Java is now about twenty years old, and the classic book, Core Java, covers, in meticulous detail, not just the language but all core libraries and a multitude of changes between versions, spanning two volumes and well over 2,000 pages. But Java 8 changes everything. Many of the old Java idioms are no longer required, and there is a much faster, easier pathway for learning Java. In this book, I show you the good parts of modern Java so you can put them to work quickly.
As with my previous Impatient books, I quickly cut to the chase, showing you what you need to know for solving a programming problem without lecturing about the superiority of one paradigm over another. I also present the information in small chunks, organized so that you can quickly retrieve it when needed.
Assuming you are proficient in some other programming language, such as C++, JavaScript, Objective C, PHP, or Ruby, with this book you will learn how to become a competent Java programmer. I cover all aspects of Java that a developer needs to know, including the powerful lambda expressions and streams that were introduced in Java 8. I tell you where to find out more about old-fashioned concepts that you might still see in legacy code, but I dont dwell on them.
A key reason to use Java is to tackle concurrent programming. With parallel algorithms and threadsafe data structures readily available in the Java library, the way application programmers should handle concurrent programming has completely changed. I provide fresh coverage, showing you how to use the powerful library features instead of error-prone, low-level constructs.
Traditional books on Java are focused on user interface programmingbut nowadays, few developers produce user interfaces on desktop computers. If you intend to use Java for server-side programming or Android programming, you will be able to use this book effectively without being distracted by desktop GUI code.
Finally, this book is written for application programmers, not for a college course and not for systems wizards. The book covers issues that application programmers need to wrestle with, such as logging and working with filesbut you wont learn how to implement a linked list by hand or how to write a web server.
I hope you enjoy this rapid-fire introduction into modern Java, and I hope it will make your work with Java productive and enjoyable.
If you find errors or have suggestions for improvement, please visit http://horstmann.com/javaimpatient
and leave a comment. On that page, you will also find a link to an archive file containing all code examples from the book.
Acknowledgments
My thanks go, as always, to my editor Greg Doench, who enthusiastically supported the vision of a short book that gives a fresh introduction to Java 8. Dmitry Kirsanov and Alina Kirsanova once again turned an XHTML manuscript into an attractive book with amazing speed and attention to detail. My special gratitude goes to the excellent team of reviewers who spotted many errors and gave thoughtful suggestions for improvement. They are: Andres Almiray, Brian Goetz, Marty Hall, Mark Lawrence, Doug Lea, Simon Ritter, Yoshiki Shibata, and Christian Ullenboom.
Cay Horstmann
Biel/Bienne, Switzerland
January 2015
About the Author
Cay S. Horstmann is the author of Java SE 8 for the Really Impatient and Scala for the Impatient (both from Addison-Wesley), 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.
Chapter 1. Fundamental Programming Structures