Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
CAY S. HORSTMANN
GARY CORNELL
VOLUME I-FUNDAMENTALS
EIGHTH EDITION
Sun Microsystems Press
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.
Sun Microsystems, Inc., has intellectual property rights relating to implementations of the technology described in this publication.
In particular, and without limitation, these intellectual property rights may include one or more VS. patents, foreign patents, or
pending applications. Sun, Sun Microsystems, the Sum logo, J2ME. Solaris, Java, Javadoc. Net Beans, and all Sun and Java based
trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries.
UNIX is a registered trademark in the United States and other countries, exclusively licensed through X/Open Company, Ltd.
The authors 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 lor incidental or consequential damages in connection
with or arising out of the use of the information or programs contained herein.
THIS PUBLICATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE. OR NON-INFRINGEMENT. THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPO-
GRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL
BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC., MAY MAKE IMPROVE-
MENTS AND/OR CHANGES IN THE PRODUCTS) AND/OR THE PROCRAM(S) DESCRIBED IN THIS PUBLICATION AT
ANYTIME
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may
include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and
branding interests. For more information, please contact U5. Corporate and Government Sales, (800) 382-3419, corpsales@pear-
sontechgroup.com. For sales outside the United States please contact: International Sales, tatemational@pearsoned.com.
Visit us on the Web: www.prenhaIlprofessional.com
Library of Congress Cataloging-in-Publication Data
Horstmann. CayS.,1959-
Core Java. Volume I, Fundamentals / Cay S. Horstmann, Gary Cornell. —
8th ed.
p.
cm.
Includes index.
ISBN 978-0-13-235476-9 (pbk.: alk. paper) I. lava (Computer program
language) I. Cornell, Gary. II. Title. III. Title: Fundamentals. IV.
Title: Core-Java fundamentals.
QA76.73.I3SH6753 2008
005.133-dc22
2007028843
Copyright© 2008 Sun Microsystems, Inc.
4150 Network Circle, Santa Clara, California 95054 U.S.A.
All rights reserved.. Printed in the United States of America. This publication is protected by copyright, and permission must he
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 likeivi.se. For information regardling permissions, write to: Pearson
Education, Inc.. Rights and Contracts Department. One Like Street, Upper Saddle River, NJ 07458.
ISBN-13: 978-0-13-235476-9
ISBN-10: 0-13-235476-4
Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.
First printing, September 2007
Table of Contents
Preface xix
Acknowledgments xxv
Chapter 1: An Introduction to Java 1
Java As a Programming Platform 2
The Java “White Paper” Buzzwords 2
Java Applets and the Internet 7
A Short History of Java 9
Common Misconceptions about Java 11
Chapter 2: The Java Programming Environment 15
Installing the Java Development Kit 16
Choosing a Development Environment 21
Using the Command-Line Tools 22
Using an Integrated Development Environment 25
Running a Graphical Application 28
Building and Running Applets 31
Chapter 3: Fundamental Programming Structures in Java 35
A Simple Java Program 36
Comments 39
Data Types 40
Variables 44
Operators 46
Strings 53
Input and Output 63
Control Flow 71
Big Numbers 88
Arrays 90
Chapter 4: Objects and Classes 105
Introduction to Object-Oriented Programming 106
Using Predefined Classes 111
Defining Your Own Classes 122
Static Fields and Methods 132
Method Parameters 138
Object Construction 144
Packages 15
The Class Path 160
Documentation Comments 162
Class Design Hints 167
Chapter 5: Inheritance 171
Classes, Superclasses, and Subclasses 172
Object: The Cosmic Superclass 192
Generic Array Lists 204
Object Wrappers and Autoboxing 211
Methods with a Variable Number of Parameters 214
Enumeration Classes 215
Reflection 217
Design Hints for Inheritance 238
Chapter 6: Interfaces and Inner Classes 241
Interfaces 242
Object Cloning 249
Interfaces and Callbacks 255
Inner Classes 258
Proxies 275
Chapter 7: Graphics Programming 281
Introducing Swing 282
Creating a Frame 285
Positioning a Frame 288
Displaying Information in a Component 294
Working with 2D Shapes 299
Using Color 307
Using Special Fonts for Text 310
Displaying Images 318
Chapter 8: Event Handling 323
Basics of Event Handling 324
Actions 342
Mouse Events 349
The AWT Event Hierarchy 357
Chapter 9: User Interface Components with Swing 361
Swing and the Model-View-Controller Design Pattern 362
Introduction to Layout Management 368
Text Input 377
Choice Components 385
Menus 406
Sophisticated Layout Management 424
Dialog Boxes 452
Chapter 10: Deploying Applications and Applets 493
JAR Files 494
Java Web Start 501
Applets 516
Storage of Application Preferences 539
Chapter 11: Exceptions, Logging, Assertions, and Debugging 551
Dealing with Errors 552
Catching Exceptions 559
Tips for Using Exceptions 568
Using Assertions 571
Logging 575
Debugging Tips 591
Using a Debugger 607
- 1
- 2
前往页