Table Of ContentCompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: i
The
Java™
Complete
Twelfth Edition
Reference
00-FM.indd 1 22/09/21 6:45 PM
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
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9 / blind folio: iii
The
Java™
Complete
Twelfth Edition
Reference
Herbert Schildt
New York Chicago San Francisco
Athens London Madrid Mexico City
Milan New Delhi Singapore Sydney Toronto
00-FM.indd 3 22/09/21 6:45 PM
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.
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
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
CompRef_2010 / Java: The Complete Reference, Twelfth Edition / Schildt / 126046-341-9
Contents
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
vii
00-FM.indd 7 22/09/21 6:45 PM
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
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