Copyright © 2017 by McGraw-Hill Education. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. ISBN: 978-1-26-001138-8 MHID: 1-26-001138-0. The material in this eBook also appears in the print version of this title: ISBN: 978-1-26-001136-4, MHID: 1-26-001136-4. eBook conversion by codeMantra Version 1.0 All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in corporate training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com. Oracle and Java are registered trademarks of Oracle Corporation and/or its affiliates. All other trademarks are the property of their respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these marks. Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of Oracle Corporation and/or its affiliates. Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any information contained in this Work, and is not responsible for any errors or omissions. TERMS OF USE This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill Education’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill Education and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill Education has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. CONTRIBUTORS About the Authors Kathy Sierra was a lead developer for the SCJP exam for Java 5 and Java 6. Kathy worked as a Sun “master trainer,” and in 1997, founded JavaRanch.com (now named Coderanch.com), the world’s largest Java community website. Her bestselling Java books have won multiple Software Development Magazine awards, and she is a founding member of Oracle’s Java Champions program. These days, Kathy is developing advanced training programs in a variety of domains (from horsemanship to computer programming), but the thread that ties all her projects together is helping learners reduce cognitive load. Bert Bates was a lead developer for many of Sun’s Java certification exams, including the SCJP for Java 5 and Java 6. Bert was also one of the lead developers for Oracle’s OCA 7 and OCP 7 exams and a contributor on Oracle’s OCA 8 and OCP 8 exams. He is a forum moderator on Coderanch.com (formerly JavaRanch.com) and has been developing software for more than 30 years (argh!). Bert is the co-author of several bestselling Java books, and he’s a founding member of Oracle’s Java Champions program. Now that the book is done, Bert plans to go whack a few tennis balls around and once again start riding his beautiful Icelandic horse, Eyrraros fra Gufudal-Fremri. About the Technical Review Team This is the fifth edition of the book that we’ve cooked up. The first version we worked on was for Java 2. Then we updated the book for the SCJP 5, again for the SCJP 6, again for the OCA 7 and OCP 7 exams, and now for the OCA 8. Every step of the way, we were unbelievably fortunate to have fantastic, JavaRanch.com-centric technical review teams at our sides. Over the course of the last 14 years, we’ve been “evolving” the book more than rewriting it. Many sections from our original work on the Java 2 book are still intact. On the following pages, we’d like to acknowledge the members of the various technical review teams who have saved our bacon over the years. About the Java 2 Technical Review Team Johannes de Jong has been the leader of our technical review teams forever and ever. (He has more patience than any three people we know.) For the Java 2 book, he led our biggest team ever. Our sincere thanks go out to the following volunteers who were knowledgeable, diligent, patient, and picky, picky, picky! Rob Ross, Nicholas Cheung, Jane Griscti, Ilja Preuss, Vincent Brabant, Kudret Serin, Bill Seipel, Jing Yi, Ginu Jacob George, Radiya, LuAnn Mazza, Anshu Mishra, Anandhi Navaneethakrishnan, Didier Varon, Mary McCartney, Harsha Pherwani, Abhishek Misra, and Suman Das. About the SCJP 5 Technical Review Team We don’t know who burned the most midnight oil, but we can (and did) count everybody’s edits—so in order of most edits made, we proudly present our Superstars. Our top honors go to Kristin Stromberg—every time you see a semicolon used correctly, tip your hat to Kristin. Next up is Burk Hufnagel who fixed more code than we care to admit. Bill Mietelski and Gian Franco Casula caught every kind of error we threw at them—awesome job, guys! Devender Thareja made sure we didn’t use too much slang, and Mark Spritzler kept the humor coming. Mikalai Zaikin and Seema Manivannan made great catches every step of the way, and Marilyn de Queiroz and Valentin Crettaz both put in another stellar performance (saving our butts yet again). Marcelo Ortega, Jef Cumps (another veteran), Andrew Monkhouse, and Jeroen Sterken rounded out our crew of Superstars—thanks to you all. Jim Yingst was a member of the Sun exam creation team, and he helped us write and review some of the twistier questions in the book (bwa-ha-ha-ha). As always, every time you read a clean page, thank our reviewers, and if you do catch an error, it’s most certainly because your authors messed up. And oh, one last thanks to Johannes. You rule, dude! About the SCJP 6 Technical Review Team Since the upgrade to the Java 6 exam was like a small surgical strike we decided that the technical review team for this update to the book needed to be similarly fashioned. To that end we hand-picked an elite crew of JavaRanch’s top gurus to perform the review for the Java 6 exam. Our endless gratitude goes to Mikalai Zaikin. Mikalai played a huge role in the Java 5 book, and he returned to help us out again for this Java 6 edition. We need to thank Volha, Anastasia, and Daria for letting us borrow Mikalai. His comments and edits helped us make huge improvements to the book. Thanks, Mikalai! Marc Peabody gets special kudos for helping us out on a double header! In addition to helping us with Sun’s new SCWCD exam, Marc pitched in with a great set of edits for this book—you saved our bacon this winter, Marc! (BTW, we didn’t learn until late in the game that Marc, Bryan Basham, and Bert all share a passion for ultimate Frisbee!) Like several of our reviewers, not only does Fred Rosenberger volunteer copious amounts of his time moderating at JavaRanch, he also found time to help us out with this book. Stacey and Olivia, you have our thanks for loaning us Fred for a while. Marc Weber moderates at some of JavaRanch’s busiest forums. Marc knows his stuff and uncovered some really sneaky problems that were buried in the book. While we really appreciate Marc’s help, we need to warn you all to watch out—he’s got a Phaser! Finally, we send our thanks to Christophe Verre—if we can find him. It appears that Christophe performs his JavaRanch moderation duties from various locations around the globe, including France, Wales, and most recently Tokyo. On more than one occasion Christophe protected us from our own lack of organization. Thanks for your patience, Christophe! It’s important to know that these guys all donated their reviewer honorariums to JavaRanch! The JavaRanch community is in your debt. The OCA 7 and OCP 7 Team Contributing Authors The OCA 7 exam is primarily a useful repackaging of some of the objectives from the SCJP 6 exam. On the other hand, the OCP 7 exam introduced a vast array of brand-new topics. We enlisted several talented Java gurus to help us cover some of the new topics on the OCP 7 exam. Thanks and kudos to Tom McGinn for his fantastic work in creating the massive JDBC chapter. Several reviewers told us that Tom did an amazing job channeling the informal tone we use throughout the book. Next, thanks to Jeanne Boyarsky. Jeanne was truly a renaissance woman on this project. She contributed to several OCP chapters; she wrote some questions for the master exams; she performed some project management activities; and as if that wasn’t enough, she was one of our most energetic technical reviewers. Jeanne, we can’t thank you enough. Our thanks go to Matt Heimer for his excellent work on the Concurrent chapter. A really tough topic nicely handled! Finally, Roel De Nijs and Roberto Perillo made some nice contributions to the book and helped out on the technical review team—thanks, guys! Technical Review Team Roel, what can we say? Your work as a technical reviewer is unparalleled. Roel caught so many technical errors, it made our heads spin. Between the printed book and all the material on the CD, we estimate that there are over 1,500 pages of “stuff” here. It’s huge! Roel grinded through page after page, never lost his focus, and made this book better in countless ways. Thank you, Roel! In addition to her other contributions, Jeanne provided one of the most thorough technical reviews we received. (We think she enlisted her team of killer robots to help her!) It seems like no K&B book would be complete without help from our old friend Mikalai Zaikin. Somehow, between earning 812 different Java certifications, being a husband and father (thanks to Volha, Anastasia, Daria, and Ivan), and being a “theoretical fisherman” [sic], Mikalai made substantial contributions to the quality of the book; we’re honored that you helped us again, Mikalai. Next up, we’d like to thank Vijitha Kumara, JavaRanch moderator and tech reviewer extraordinaire. We had many reviewers help out during the long course of writing this book, but Vijitha was one of the few who stuck with us from Chapter 1 all the way through the master exams and on to Chapter 15. Vijitha, thank you for your help and persistence! Finally, thanks to the rest of our review team: Roberto Perillo (who also wrote some killer exam questions), Jim Yingst (was this your fourth time?), other repeat offenders: Fred Rosenberger, Christophe Verre, Devaka Cooray, Marc Peabody, and newcomer Amit Ghorpade—thanks, guys! About the OCA 8 Technical Review Team Due to “pilot error” on your authors’ part, the members of the review team for this book were asked to work under a tighter-than-ideal schedule. We owe a huge thanks to our six reviewers for this book, who leapt to action on short notice and once again made numerous fine contributions to the quality of the book. As has become “the norm,” all our reviewers are moderators at the finest Java community website in the whole universe, Coderanch.com. Our first mention goes to Campbell Ritchie, long-time Coderanch moderator, hiker of steep hills, and, by our count, most prolific reviewer for this edition. In other words, Campbell found the most errors. Among other pursuits, Campbell is an expert in both pathology and programming, because…why not? Every time you read an error-free page, think of Campbell and thank him. In a virtual tie for second place in the “errors found” competition, we present Paweł Baczyński and Fritz Walraven. Paweł calls Poland his home and is most proud of his wife, Ania, and kids, Szymek and