Statistics
4
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-02-03

AuthorCay S. Horstmann

The Classic Guide to Advanced Java Programming: Fully Updated for Java 21 Core Java is the leading no-nonsense tutorial and reference for experienced programmers who want to write robust Java code for real-world applications. Now, Core Java, Volume II: Fundamentals, Thirteenth Edition, has been revised to cover the new features and enhancements in the Java 21 long-term support release. As always, all chapters have been completely updated, outdated material has been removed, and the new APIs are covered in detail. This volume focuses on the advanced topics that a programmer needs to know for professional software development and includes authoritative coverage of enterprise programming, networking, databases, security, internationalization, and native methods, as well as complete chapters on the Streams, XML, Date and Time, Scripting, and Compilation APIs. In addition, the chapters on Swing and Graphics cover techniques that are applicable to both client-side user interfaces and server-side generation of graphics and images. Cay S. Horstmann clearly explains sophisticated new features with depth and completeness and demonstrates how to use them to build professional-quality applications. Horstmann's thoroughly tested sample code reflects modern Java style and best practices. The examples are carefully crafted for easy understanding and maximum practical value, so you can rely on them to jump-start your own programs. Master advanced techniques, idioms, and best practices for writing reliable Java code Make the most of enhanced Java I/O APIs, object serialization, and regular expressions Connect to network services, harvest web data with the HTTP/2 client, and serve data with the built-in web server or implement your own server Process code via the Scripting and Compiler APIs Work with the Date and Time API, including recent refinements Leverage the Java security model, user authentication, and the security library's cryptographic functions Preview powerful new APIs fo

Tags
No tags
ISBN: 0135371732
Publisher: Addison-Wesley Professional
Publish Year: 2024
Language: 英文
File Format: PDF
File Size: 20.0 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
1. About This eBook ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturer’s Web site. Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the eBook in single-column, landscape mode and adjust the font size to the smallest setting.
Core Java Volume II—Advanced Features Thirteenth Edition Cay S. Horstmann Hoboken, New Jersey Cover image: Leyland/Shutterstock Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with
or arising out of the use of the information or programs contained herein. The views expressed in this book are those of the author and do not necessarily reflect the views of Oracle. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419. For government sales inquiries, please contact governmentsales@pearsoned.com. For questions about sales outside the U.S., please contact intlcs@pearson.com. Please contact us with concerns about any potential bias at pearson.com/report-bias.html. Visit us on the Web: informit.com/aw Copyright © 2024 Pearson Education, Inc. Portions copyright © 1996-2013 Oracle and/or its affiliates. All Rights Reserved.
Oracle America Inc. 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. Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the documents and related graphics published as part of the services for any purpose. All such documents and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event shall Microsoft and/or its respective suppliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services. The documents and related graphics contained herein could include technical inaccuracies or typographical errors. Changes are periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at
any time. Partial screen shots may be viewed in full within the software version specified. Microsoft® Windows®, and Microsoft Office® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearson.com/permissions. ISBN-13: 978-0-13-537172-5 ISBN-10: 0-13-537172-4
1. Table of Contents 1. Preface 1. To the Reader 2. A Tour of This Book 3. Conventions 2. Acknowledgments 1. 1.1. From Iterating to Stream Operations 2. 1.2. Stream Creation 3. 1.3. The filter , map , and flatMap Methods 4. 1.4. Extracting Substreams and Combining Streams 5. 1.5. Other Stream Transformations 6. 1.6. Simple Reductions 7. 1.7. The Optional Type 8. 1.8. Collecting Results 9. 1.9. Collecting into Maps 10. 1.10. Grouping and Partitioning 11. 1.11. Downstream Collectors 12. 1.12. Reduction Operations 13. 1.13. Primitive Type Streams 14. 1.14. Parallel Streams 3. 2. Input and Output 1. 2.1. Input/Output Streams 2. 2.2. Reading and Writing Binary Data
3. 2.3. Object Input/Output Streams and Serialization 4. 2.4. Working with Files 5. 2.5. Memory-Mapped Files 6. 2.6. File Locking 7. 2.7. Regular Expressions 4. 3. XML 1. 3.1. Introducing XML 2. 3.2. The Structure of an XML Document 3. 3.3. Parsing an XML Document 4. 3.4. Validating XML Documents 5. 3.5. Locating Information with XPath 6. 3.6. Using Namespaces 7. 3.7. Streaming Parsers 8. 3.8. Generating XML Documents 9. 3.9. XSL Transformations 5. 4. Networking 1. 4.1. Connecting to a Server 2. 4.2. Implementing Servers 3. 4.3. Getting Web Data 4. 4.4. The HTTP Client 5. 4.5. The Simple HTTP Server 6. 4.6. Sending E-Mail 6. 5. Database Programming 1. 5.1. The Design of JDBC
2. 5.2. The Structured Query Language 3. 5.3. JDBC Configuration 4. 5.4. Working with JDBC Statements 5. 5.5. Query Execution 6. 5.6. Scrollable and Updatable Result Sets 7. 5.7. Row Sets 8. 5.8. Metadata 9. 5.9. Transactions 10. 5.10. Connection Management in Web and Enterprise Applications 7. 6. The Date and Time API 1. 6.1. The Time Line 2. 6.2. Local Dates 3. 6.3. Date Adjusters 4. 6.4. Local Time 5. 6.5. Zoned Time 6. 6.6. Formatting and Parsing 7. 6.7. Interoperating with Legacy Code 8. 7. Internationalization 1. 7.1. Locales 2. 7.2. Number Formats 3. 7.3. Date and Time 4. 7.4. Collation and Normalization 5. 7.5. Message Formatting
6. 7.6. Text Boundaries 7. 7.7. Text Input and Output 8. 7.8. Resource Bundles 9. 7.9. A Complete Example 9. 8. Compiling and Scripting 1. 8.1. The Compiler API 2. 8.2. Scripting for the Java Platform 10. 9. Security 1. 9.1. Class Loaders 2. 9.2. User Authentication 3. 9.3. Digital Signatures 4. 9.4. Encryption 11. 10. Graphical User Interface Programming 1. 10.1. A History of Java User Interface Toolkits 2. 10.2. Displaying Frames 3. 10.3. Displaying Information in a Component 4. 10.4. Event Handling 5. 10.5. The Preferences API 12. 11. User Interface Components with Swing 1. 11.1. Swing and the Model-View-Controller Design Pattern 2. 11.2. Introduction to Layout Management 3. 11.3. Text Input 4. 11.4. Choice Components 5. 11.5. Menus
6. 11.6. The Grid Bag Layout 7. 11.7. Custom Layout Managers 8. 11.8. Dialog Boxes 13. 12. Advanced Swing and Graphics 1. 12.1. Tables 2. 12.2. Working with Rows and Columns 3. 12.3. Cell Rendering and Editing 4. 12.4. Trees 5. 12.5. Advanced AWT 6. 12.6. Raster Images 7. 12.7. Printing 14. 13. Native Methods 1. 13.1. Calling a C Function from a Java Program 2. 13.2. Numeric Parameters and Return Values 3. 13.3. String Parameters 4. 13.4. Accessing Fields 5. 13.5. Encoding Signatures 6. 13.6. Calling Java Methods 7. 13.7. Accessing Array Elements 8. 13.8. Handling Errors 9. 13.9. Using the Invocation API 10. 13.10. A Complete Example: Accessing the Windows Registry 11. 13.11. Foreign Functions: A Glimpse into the Future
1. Preface 1.1. To the Reader The book you have in your hands is the second volume of the thirteenth edition of Core Java, fully updated for Java 21. The first volume covers the essential features of the language; this volume deals with the advanced topics that a programmer needs to know for professional software development. Thus, as with the first volume and the previous editions of this book, we are still targeting programmers who want to put Java technology to work in real projects. With the explosive growth of the Java class library, a one-volume treatment of all the features of Java that serious programmers need to know is simply not possible. Hence, the book is broken up into two volumes. This first volume concentrates on the fundamental concepts of the Java language: Object-oriented programming Reflection and proxies Interfaces and inner classes Exception handling
Generic programming The collections framework Concurrency Annotations The Java platform module system This second volume goes further into the most important libraries. For twelve editions, user-interface programming was considered fundamental, but the time has come to recognize that it is no more, and to move it into the second volume. The volume includes detailed discussions of these topics: The Stream API File processing and regular expressions Databases XML processing Scripting and Compiling APIs Internationalization Network programming Graphical user interface design Graphics programming Native methods As is the case with any book, errors and inaccuracies are inevitable. Should you find any in this book, we would very much like to hear
about them. Of course, we would prefer to hear about them only once. For this reason, we have put up a web site at https://horstmann.com/corejava with a FAQ, bug fixes, and workarounds. Strategically placed at the end of the bug report web page (to encourage you to read the previous reports) is a form that you can use to report bugs or problems and to send suggestions for improvements for future editions.
1.2. A Tour of This Book The chapters in this book are, for the most part, independent of each other. You should be able to delve into whatever topic interests you the most and read the chapters in any order. In Chapter 1, you will learn all about the Java stream library that brings a modern flavor to processing data, by specifying what you want without describing in detail how the result should be obtained. This allows the stream library to focus on an optimal evaluation strategy, which is particularly advantageous for executing computations in parallel with multiple cores. The topic of Chapter 2 is input and output handling (I/O). In Java, all input and output is handled through input/output streams. These streams (not to be confused with those in Chapter 1) let you deal, in a uniform manner, with communications among various sources of data, such as files, network connections, or memory blocks. We include detailed coverage of the reader and writer classes that make it easy to deal with Unicode. We show you what goes on under the hood when you use the object serialization mechanism, which makes saving and loading objects easy and convenient. We then move on to regular expressions and working with files and paths. Throughout this chapter, you will find welcome enhancements in recent Java versions.
Chapter 3 covers XML. We show you how to parse XML files, how to generate XML, and how to use XSL transformations. As a useful example, we show you how to specify the layout of a Swing form in XML. We also discuss the XPath API, which makes finding needles in XML haystacks much easier. Chapter 4 covers the networking API. Java makes it phenomenally easy to do complex network programming. We show you how to make network connections to servers, how to implement your own servers, and how to make HTTP connections. This chapter includes coverage of the new HTTP client. Chapter 5 covers database programming. The focus is on JDBC, the Java database connectivity API that lets Java programs connect to relational databases. We show you how to write useful programs to handle realistic database chores, using a core subset of the JDBC API. (A complete treatment of the JDBC API would require a book almost as big as this one.) Java had two prior attempts at libraries for handling date and time. The third one was the charm in Java 8. In Chapter 6, you will learn how to deal with the complexities of calendars and time zones, using the new date and time library.
Chapter 7 discusses a feature that we believe can only grow in importance: internationalization. The Java programming language is one of the few languages designed from the start to handle Unicode, but the internationalization support on the Java platform goes much further. As a result, you can internationalize Java applications so that they cross not only platforms but country boundaries as well. For example, we show you how to write a retirement calculator that uses either English, German, or Chinese languages. Chapter 8 discusses the scripting and compiler APIs that allow your program to call code in scripting languages such as JavaScript or Groovy, and to compile Java code. Chapter 9 takes up the Java security model, user authentication, and the cryptographic functions in the Java security library. You will learn about important features such as message and code signing, authorization and authentication, and encryption. We conclude with examples that use the AES and RSA encryption algorithms. Chapter 10 provides an introduction into GUI programming. I show how you can make windows, how to paint on them, how to draw with geometric shapes, how to format text in multiple fonts, and how to display images. Next, you'll see how to write code that responds to events, such as mouse clicks or key presses.
Chapter 11 discusses the Swing GUI toolkit in great detail. The Swing toolkit allows you to build cross-platform graphical user interfaces. You'll learn all about the various kinds of buttons, text components, borders, sliders, list boxes, menus, and dialog boxes. Chapter 12 contains advanced Swing material, especially the important but complex tree and table components. We also cover the Java 2D API, which you can use to create realistic drawings and special effects. Of course, not many programmers need to program Swing user interfaces these days, so we pay particular attention to features that are useful for images that can be generated on a server. Chapter 13 takes up native methods, which let you call methods written for a specific machine such as the Microsoft Windows API. Obviously, this feature is controversial: Use native methods, and the cross-platform nature of Java vanishes. Nonetheless, every serious programmer writing Java applications for specific platforms needs to know these techniques. At times, you need to turn to the operating system's API for your target platform when you interact with a device or service that is not supported by Java. We illustrate this by showing you how to access the registry API in Windows from a Java program. The chapter ends with a glimpse of the “Panama” API which will replace the current API in a future version of Java.
As always, all chapters have been completely revised for the latest version of Java. Outdated material has been removed, and the new APIs up to Java 21 are covered in detail. 1.3. Conventions As is common in many computer books, I use monospace type to represent computer code. Note: Notes are tagged with "note" icons that look like this. Tip: Tips are tagged with "tip" icons that look like this. Caution: When there is danger ahead, I warn you with a "caution" icon. Preview Note: Preview features that are slated to become a part of the language or API in the future are labeled with this icon. C++ Note: There are a number of C++ notes that explain the difference between the Java programming language and C++. You
can skip them if you aren't interested in C++. Java comes with a large programming library, or Application Programming Interface (API). When using an API call for the first time, I add a short summary description at the end of the section. These descriptions are a bit more informal but, hopefully, also a little more informative than those in the official online API documentation. The names of interfaces are in italics, just like in the official documentation. The number after a class, interface, or method name is the JDK version in which the feature was introduced, as shown in the following example: Application Programming Interface 1.2 Programs whose source code is included in the companion code for this book are listed as examples, for instance Listing 1.1 NotHelloWorld.java 1 void main() 2 { 3 System.out.println("We will not use 'Hello, 4 }