📄 Page
1
(This page has no text content)
📄 Page
2
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: i The Complete Reference Java™ Twelfth Edition 00-FM.indd 1 22/09/21 6:45 PM
📄 Page
3
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: ii About the Author Best-selling author Herbert Schildt has written extensively about programming for over three decades and is a leading authority on the Java language. Called “one of the world’s foremost authors of books about programming” by International Developer magazine, his books have sold millions of copies worldwide and have been translated into all major foreign languages. He is the author of numerous books on Java, including Java: A Beginner’s Guide, Herb Schildt’s Java Programming Cookbook, Introducing JavaFX 8 Programming, and Swing: A Beginner’s Guide. He has also written extensively about C, C++, and C#. Featured as one of the rock star programmers in Ed Burns’ book Secrets of the Rock Star Programmers: Riding the IT Crest, Schildt is interested in all facets of computing, but his primary focus is computer languages. Schildt holds both BA and MCS degrees from the University of Illinois. His website is www.HerbSchildt.com. About the Technical Editor Dr. Danny Coward has worked on all editions of the Java platform. He led the definition of Java Servlets into the first version of the Java EE platform and beyond, web services into the Java ME platform, and the strategy and planning for Java SE 7. He founded JavaFX technology and, most recently, designed the largest addition to the Java EE 7 standard, the Java WebSocket API. From coding in Java, to designing APIs with industry experts, to serving for several years as an executive to the Java Community Process, he has a uniquely broad perspective into multiple aspects of Java technology. In addition, he is the author of two books on Java programming: Java WebSocket Programming and Java EE 7: The Big Picture. Most recently, he has been applying his knowledge of Java to helping scale massive Java-based services for one of the world’s most successful software companies. Dr. Coward holds a bachelor’s, master’s, and doctorate in mathematics from the University of Oxford. 00-FM.indd 2 22/09/21 6:45 PM
📄 Page
4
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: iii The Complete Reference Herbert Schildt New York Chicago San Francisco Athens London Madrid Mexico City Milan New Delhi Singapore Sydney Toronto Java™ Twelfth Edition 00-FM.indd 3 22/09/21 6:45 PM
📄 Page
5
Copyright © 2022 by McGraw Hill. 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. ISBN: 978-1-26-046342-2 MHID: 1-26-046342-7 The material in this eBook also appears in the print version of this title: ISBN: 978-1-26-046341-5, MHID: 1-26-046341-9. 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. Information has been obtained by McGraw Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw Hill, or others, McGraw Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any in- formation 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, IN- CLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICU- LAR 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.
📄 Page
6
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents at a Glance PART I The Java Language 1 The History and Evolution of Java 3 2 An Overview of Java 21 3 Data Types, Variables, and Arrays 39 4 Operators 67 5 Control Statements 87 6 Introducing Classes 117 7 A Closer Look at Methods and Classes 137 8 Inheritance 171 9 Packages and Interfaces 199 10 Exception Handling 227 11 Multithreaded Programming 247 12 Enumerations, Autoboxing, and Annotations 277 13 I/O, Try-with-Resources, and Other Topics 315 14 Generics 347 15 Lambda Expressions 391 16 Modules 421 17 Switch Expressions, Records, and Other Recently Added Features 449 PART II The Java Library 18 String Handling 483 19 Exploring java.lang 511 20 java.util Part 1: The Collections Framework 571 21 java.util Part 2: More Utility Classes 653 22 Input/Output: Exploring java.io 713 23 Exploring NIO 763 24 Networking 795 25 Event Handling 819 26 Introducing the AWT: Working with Windows, Graphics, and Text 851 27 Using AWT Controls, Layout Managers, and Menus 879 28 Images 929 29 The Concurrency Utilities 955 30 The Stream API 1005 31 Regular Expressions and Other Packages 1031 v 00-FM.indd 5 22/09/21 6:45 PM
📄 Page
7
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 vi Java: The Complete Reference PART III Introducing GUI Programming with Swing 32 Introducing Swing 1061 33 Exploring Swing 1079 34 Introducing Swing Menus 1109 PART IV Applying Java 35 Java Beans 1145 36 Introducing Servlets 1157 PART V Appendixes A Using Java’s Documentation Comments 1183 B Introducing JShell 1191 C Compile and Run Simple Single-File Programs in One Step 1201 Index 1203 00-FM.indd 6 22/09/21 6:45 PM
📄 Page
8
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 vii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi Part I The Java Language Chapter 1 The History and Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Java’s Lineage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Birth of Modern Programming: C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 C++: The Next Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Stage Is Set for Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The Creation of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The C# Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 How Java Impacted the Internet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Java Applets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Java’s Magic: The Bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Moving Beyond Applets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 A Faster Release Schedule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Servlets: Java on the Server Side. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 The Java Buzzwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Object-Oriented. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Robust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Multithreaded. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Architecture-Neutral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Interpreted and High Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Distributed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Evolution of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 A Culture of Innovation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter 2 An Overview of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Two Paradigms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 The Three OOP Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Contents 00-FM.indd 7 22/09/21 6:45 PM
📄 Page
9
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 viii Java: The Complete Reference A First Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Entering the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 A Closer Look at the First Sample Program . . . . . . . . . . . . . . . . . . . . . . . 28 A Second Short Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Two Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Using Blocks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Lexical Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The Java Keywords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The Java Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Chapter 3 Data Types, Variables, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 Java Is a Strongly Typed Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 The Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 short . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 long. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Floating-Point Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 float. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 A Closer Look at Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Dynamic Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Type Conversion and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Java’s Automatic Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Casting Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 00-FM.indd 8 22/09/21 6:45 PM
📄 Page
10
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents ix Automatic Type Promotion in Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 The Type Promotion Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Alternative Array Declaration Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Introducing Type Inference with Local Variables . . . . . . . . . . . . . . . . . . . . . . . 62 Some var Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 A Few Words About Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter 4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 The Basic Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 The Modulus Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Arithmetic Compound Assignment Operators . . . . . . . . . . . . . . . . . . . . 69 Increment and Decrement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 The Left Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Unsigned Right Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Bitwise Operator Compound Assignments . . . . . . . . . . . . . . . . . . . . . . . 79 Relational Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Boolean Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Short-Circuit Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The ? Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Chapter 5 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Java’s Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 The Traditional switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 do-while. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 The For-Each Version of the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Local Variable Type Inference in a for Loop. . . . . . . . . . . . . . . . . . . . . . 108 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Using break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Using continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 00-FM.indd 9 22/09/21 6:45 PM
📄 Page
11
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 x Java: The Complete Reference Chapter 6 Introducing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Class Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 The General Form of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 A Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Declaring Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 A Closer Look at new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Assigning Object Reference Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Introducing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Adding a Method to the Box Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Adding a Method That Takes Parameters . . . . . . . . . . . . . . . . . . . . . . . . 127 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Parameterized Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Instance Variable Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 A Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Chapter 7 A Closer Look at Methods and Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Overloading Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Overloading Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Using Objects as Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 A Closer Look at Argument Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Returning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Introducing Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Understanding static. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Introducing final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Arrays Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Introducing Nested and Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Exploring the String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Using Command-Line Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Varargs: Variable-Length Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Overloading Vararg Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Varargs and Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Local Variable Type Inference with Reference Types . . . . . . . . . . . . . . . . . . . 168 Chapter 8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171 Inheritance Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Member Access and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 A More Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 A Superclass Variable Can Reference a Subclass Object . . . . . . . . . . . 176 Using super. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Using super to Call Superclass Constructors . . . . . . . . . . . . . . . . . . . . . 177 A Second Use for super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 00-FM.indd 10 22/09/21 6:45 PM
📄 Page
12
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents xi Creating a Multilevel Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 When Constructors Are Executed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Method Overriding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Dynamic Method Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Why Overridden Methods? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Applying Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Using Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Using final with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Using final to Prevent Overriding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Using final to Prevent Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Local Variable Type Inference and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . 195 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Chapter 9 Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Defining a Package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Finding Packages and CLASSPATH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 A Short Package Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Packages and Member Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 An Access Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Importing Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Defining an Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Nested Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Applying Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Variables in Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Interfaces Can Be Extended. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Default Interface Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Default Method Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 A More Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Multiple Inheritance Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Use static Methods in an Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Private Interface Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Final Thoughts on Packages and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Chapter 10 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Exception-Handling Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Exception Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Uncaught Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Using try and catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Displaying a Description of an Exception . . . . . . . . . . . . . . . . . . . . . . . . 232 Multiple catch Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Nested try Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 throws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 00-FM.indd 11 22/09/21 6:45 PM
📄 Page
13
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 xii Java: The Complete Reference finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Java’s Built-in Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Creating Your Own Exception Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Chained Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Three Additional Exception Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Chapter 11 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 The Java Thread Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 The Thread Class and the Runnable Interface . . . . . . . . . . . . . . . . . . . . 250 The Main Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Creating a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Implementing Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Extending Thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Choosing an Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Creating Multiple Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Using isAlive( ) and join( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Synchronization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Using Synchronized Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The synchronized Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Interthread Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Suspending, Resuming, and Stopping Threads . . . . . . . . . . . . . . . . . . . . . . . . 272 Obtaining a Thread’s State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Using a Factory Method to Create and Start a Thread. . . . . . . . . . . . . . . . . . 275 Using Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Chapter 12 Enumerations, Autoboxing, and Annotations . . . . . . . . . . . . . . . . . . . . .277 Enumerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Enumeration Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 The values( ) and valueOf( ) Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Java Enumerations Are Class Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Enumerations Inherit Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Another Enumeration Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Character. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 The Numeric Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Autoboxing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Autoboxing and Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Autoboxing/Unboxing Occurs in Expressions . . . . . . . . . . . . . . . . . . . . 291 Autoboxing/Unboxing Boolean and Character Values. . . . . . . . . . . . . 292 00-FM.indd 12 22/09/21 6:45 PM
📄 Page
14
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents xiii Autoboxing/Unboxing Helps Prevent Errors . . . . . . . . . . . . . . . . . . . . . 293 A Word of Warning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Annotation Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Specifying a Retention Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Obtaining Annotations at Run Time by Use of Reflection. . . . . . . . . . 296 The AnnotatedElement Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Marker Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Single-Member Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 The Built-In Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Type Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Repeating Annotations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Some Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Chapter 13 I/O, Try-with-Resources, and Other Topics . . . . . . . . . . . . . . . . . . . . . . .315 I/O Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Byte Streams and Character Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Reading Console Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Reading Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Reading Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Writing Console Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 The PrintWriter Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Automatically Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 The transient and volatile Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Introducing instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 strictfp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Native Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Using assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Assertion Enabling and Disabling Options . . . . . . . . . . . . . . . . . . . . . . . 339 Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Invoking Overloaded Constructors Through this( ) . . . . . . . . . . . . . . . . . . . . 342 A Word About Value-Based Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Chapter 14 Generics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347 What Are Generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 A Simple Generics Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Generics Work Only with Reference Types . . . . . . . . . . . . . . . . . . . . . . 352 Generic Types Differ Based on Their Type Arguments . . . . . . . . . . . . 352 How Generics Improve Type Safety. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 A Generic Class with Two Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 355 The General Form of a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Bounded Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 00-FM.indd 13 22/09/21 6:45 PM
📄 Page
15
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 xiv Java: The Complete Reference Using Wildcard Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Bounded Wildcards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Creating a Generic Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 Generic Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Generic Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 Raw Types and Legacy Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Generic Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Using a Generic Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 A Generic Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Run-Time Type Comparisons Within a Generic Hierarchy . . . . . . . . 378 Casting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Overriding Methods in a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . 380 Type Inference with Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Local Variable Type Inference and Generics . . . . . . . . . . . . . . . . . . . . . . . . . . 383 Erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 Bridge Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Ambiguity Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Some Generic Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Type Parameters Can’t Be Instantiated . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Restrictions on Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Generic Array Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Generic Exception Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 Chapter 15 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .391 Introducing Lambda Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Lambda Expression Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Functional Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Some Lambda Expression Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Block Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Generic Functional Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 Passing Lambda Expressions as Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Lambda Expressions and Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 Lambda Expressions and Variable Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 Method References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Method References to static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Method References to Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . 407 Method References with Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Constructor References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 Predefined Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Chapter 16 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421 Module Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 A Simple Module Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 Compile and Run the First Module Example . . . . . . . . . . . . . . . . . . . . . 426 A Closer Look at requires and exports . . . . . . . . . . . . . . . . . . . . . . . . . . 428 00-FM.indd 14 22/09/21 6:45 PM
📄 Page
16
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents xv java.base and the Platform Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Legacy Code and the Unnamed Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Exporting to a Specific Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 Using requires transitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 Use Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Service and Service Provider Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 The Service-Based Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 A Module-Based Service Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Module Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Three Specialized Module Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Open Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 The opens Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 requires static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Introducing jlink and Module JAR Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Linking Files in an Exploded Directory . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Linking Modular JAR Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 JMOD Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 A Brief Word About Layers and Automatic Modules . . . . . . . . . . . . . . . . . . 448 Final Thoughts on Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 Chapter 17 Switch Expressions, Records, and Other Recently Added Features . . . . 449 Enhancements to switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 Use a List of case Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 Introducing the switch Expression and the yield Statement . . . . . . . . 452 Introducing the Arrow in a case Statement . . . . . . . . . . . . . . . . . . . . . . 454 A Closer Look at the Arrow case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Another switch Expression Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Text Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Text Block Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Understanding Leading Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 Use Double Quotes in a Text Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 Escape Sequences in Text Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Record Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Create Record Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 Another Record Constructor Example . . . . . . . . . . . . . . . . . . . . . . . . . . 470 Create Record Getter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 Pattern Matching with instanceof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Pattern Variables in a Logical AND Expression. . . . . . . . . . . . . . . . . . . 474 Pattern Matching in Other Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Sealed Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Sealed Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Sealed Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 Future Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 00-FM.indd 15 22/09/21 6:45 PM
📄 Page
17
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 xvi Java: The Complete Reference Part II The Java Library Chapter 18 String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483 The String Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 Special String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 String Concatenation with Other Data Types . . . . . . . . . . . . . . . . . . . . 487 String Conversion and toString( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 Character Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 charAt( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 getChars( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 getBytes( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 toCharArray( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 String Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 equals( ) and equalsIgnoreCase( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 regionMatches( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 startsWith( ) and endsWith( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 equals( ) Versus == . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 compareTo( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Searching Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494 Modifying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 substring( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 concat( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 trim( ) and strip( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498 Data Conversion Using valueOf( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 Changing the Case of Characters Within a String . . . . . . . . . . . . . . . . . . . . . 499 Joining Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 Additional String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 StringBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 StringBuffer Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 length( ) and capacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 ensureCapacity( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 setLength( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 charAt( ) and setCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 getChars( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 append( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 insert( ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 reverse( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 delete( ) and deleteCharAt( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 replace( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 substring( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Additional StringBuffer Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 00-FM.indd 16 22/09/21 6:45 PM
📄 Page
18
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents xvii Chapter 19 Exploring java.lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .511 Primitive Type Wrappers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 Double and Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512 Understanding isInfinite( ) and isNaN( ) . . . . . . . . . . . . . . . . . . . . . . . . . 516 Byte, Short, Integer, and Long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Character. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526 Additions to Character for Unicode Code Point Support . . . . . . . . . . 528 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 Void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532 Executing Other Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534 Runtime.Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535 ProcessBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 Using currentTimeMillis( ) to Time Program Execution . . . . . . . . . . . 540 Using arraycopy( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 Environment Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 System.Logger and System.LoggerFinder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 Using clone( ) and the Cloneable Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 ClassLoader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 Exponential Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 Rounding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550 Miscellaneous Math Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 StrictMath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 Thread, ThreadGroup, and Runnable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 The Runnable Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 ThreadGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556 ThreadLocal and InheritableThreadLocal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 ModuleLayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562 RuntimePermission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562 Throwable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562 SecurityManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562 StackTraceElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563 StackWalker and StackWalker.StackFrame. . . . . . . . . . . . . . . . . . . . . . . . . . . . 564 Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564 Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 00-FM.indd 17 22/09/21 6:45 PM
📄 Page
19
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 xviii Java: The Complete Reference ClassValue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 The CharSequence Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 The Comparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566 The Appendable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566 The Iterable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566 The Readable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 The AutoCloseable Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 The Thread.UncaughtExceptionHandler Interface . . . . . . . . . . . . . . . . . . . . . 567 The java.lang Subpackages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 java.lang.annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 java.lang.constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 java.lang.instrument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 java.lang.invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 java.lang.management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 java.lang.module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 java.lang.ref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 java.lang.reflect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Chapter 20 java.util Part 1: The Collections Framework . . . . . . . . . . . . . . . . . . . . . . .571 Collections Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572 The Collection Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 The Collection Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 The List Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 The Set Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 The SortedSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 The NavigableSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 The Queue Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582 The Deque Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 The Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 The LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 The HashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590 The LinkedHashSet Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591 The TreeSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 The PriorityQueue Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593 The ArrayDeque Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594 The EnumSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 Accessing a Collection via an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 Using an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 The For-Each Alternative to Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 Spliterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600 Storing User-Defined Classes in Collections . . . . . . . . . . . . . . . . . . . . . . . . . . 602 The RandomAccess Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 Working with Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 The Map Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605 The Map Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612 00-FM.indd 18 22/09/21 6:45 PM
📄 Page
20
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 Contents xix Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616 Using a Comparator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 The Collection Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 The Legacy Classes and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636 The Enumeration Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636 Vector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 Hashtable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Using store( ) and load( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 Parting Thoughts on Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 Chapter 21 java.util Part 2: More Utility Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .653 StringTokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 BitSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 Optional, OptionalDouble, OptionalInt, and OptionalLong . . . . . . . . . . . . 658 Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662 GregorianCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 TimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667 SimpleTimeZone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 Random. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670 Timer and TimerTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672 Currency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 Formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676 The Formatter Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676 The Formatter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 Formatting Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 Formatting Strings and Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679 Formatting Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679 Formatting Time and Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 The %n and %% Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683 Specifying a Minimum Field Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683 Specifying Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 Using the Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 Justifying Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 The Space, +, 0, and ( Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687 The Comma Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688 The # Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688 The Uppercase Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688 Using an Argument Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 Closing a Formatter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690 The Java printf( ) Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 00-FM.indd 19 22/09/21 6:45 PM