Core Java
Volume IFundamentals
Ninth Edition
Cay S. Horstmann
Gary Cornell
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
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 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 for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
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:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/ph
Cataloging-in-Publication Data is on file with the Library of Congress.
Copyright 2013 Oracle and/or its affiliates. All rights reserved.
500 Oracle Parkway, Redwood Shores, CA 94065
All rights reserved. Printed in the United States of America. 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. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-13-708189-9
ISBN-10: 0-13-708189-8
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan.
First printing, November 2012
Contents
Preface
To the Reader
In late 1995, the Java programming language burst onto the Internet scene and gained instant celebrity status. The promise of Java technology was that it would become the universal glue that connects users with information wherever it comes fromweb servers, databases, information providers, or any other imaginable source. Indeed, Java is in a unique position to fulfill this promise. It is an extremely solidly engineered language that has gained acceptance by all major vendors, except for Microsoft. Its built-in security and safety features are reassuring both to programmers and to the users of Java programs. Java even has built-in support for advanced programming tasks, such as network programming, database connectivity, and multithreading.
Since 1995, eight major revisions of the Java Development Kit have been released. Over the course of the last 17 years, the Application Programming Interface (API) has grown from about 200 to over 3,000 classes. The API now spans such diverse areas as user interface construction, database management, internationalization, security, and XML processing.
The book you have in your hands is the first volume of the ninth edition of Core Java . Each edition closely followed a release of the Java Development Kit, and each time, we rewrote the book to take advantage of the newest Java features. This edition has been updated to reflect the features of Java Standard Edition (SE) 7.
As with the previous editions of this book, we still target serious programmers who want to put Java to work on real projects . We think of you, our reader, as a programmer with a solid background in a programming language other than Java, and we assume that you dont like books filled with toy examples (such as toasters, zoo animals, or nervous text). You wont find any of these in our book. Our goal is to enable you to fully understand the Java language and library, not to give you an illusion of understanding.
In this book you will find lots of sample code demonstrating almost every language and library feature that we discuss. We keep the sample programs purposefully simple to focus on the major points, but, for the most part, they arent fake and they dont cut corners. They should make good starting points for your own code.
We assume you are willing, even eager, to learn about all the advanced features that Java puts at your disposal. For example, we give you a detailed treatment of
Object-oriented programmingReflection and proxiesInterfaces and inner classesThe event listener modelGraphical user interface design with the Swing UI toolkitException handlingGeneric programmingThe collections frameworkConcurrency
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 no longer possible. Hence, we decided to break up the book into two volumes. The first volume, which you hold in your hands, concentrates on the fundamental concepts of the Java language, along with the basics of user-interface programming. The second volume, Core Java, Volume IIAdvanced Features (forthcoming, ISBN: 978-0-13-708160-8), goes further into the enterprise features and advanced user-interface programming. It includes detailed discussions of
Files and streamsDistributed objectsDatabasesAdvanced GUI componentsNative methodsXML processingNetwork programmingAdvanced graphicsInternationalizationJavaBeansAnnotations
When writing a book, errors and inaccuracies are inevitable. Wed very much like to know about them. But, of course, wed prefer to learn about each of them only once. We have put up a list of frequently asked questions, bugs fixes, and workarounds on a web page at http://horstmann.com/corejava. Strategically placed at the end of the errata page (to encourage you to read through it first) is a form you can use to report bugs and suggest improvements. Please dont be disappointed if we dont answer every query or dont get back to you immediately. We do read all e-mail and appreciate your input to make future editions of this book clearer and more informative.
A Tour of This Book
gives an overview of the capabilities of Java that set it apart from other programming languages. We explain what the designers of the language set out to do and to what extent they succeeded. Then, we give a short history of how Java came into being and how it has evolved.
In , we tell you how to download and install the JDK and the program examples for this book. Then we guide you through compiling and running three typical Java programsa console application, a graphical application, and an appletusing the plain JDK, a Java-enabled text editor, and a Java IDE.
starts the discussion of the Java language. In this chapter, we cover the basics: variables, loops, and simple functions. If you are a C or C++ programmer, this is smooth sailing because the syntax for these language features is essentially the same as in C. If you come from a non-C background such as Visual Basic, you will want to read this chapter carefully.
Object-oriented programming (OOP) is now in the mainstream of programming practice, and Java is an object-oriented programming language. introduces encapsulation, the first of two fundamental building blocks of object orientation, and the Java language mechanism to implement itthat is, classes and methods. In addition to the rules of the Java language, we also give advice on sound OOP design. Finally, we cover the marvelous javadoc tool that formats your code comments as a set of hyperlinked web pages. If you are familiar with C++, you can browse through this chapter quickly. Programmers coming from a non-object-oriented background should expect to spend some time mastering the OOP concepts before going further with Java.
Next page