The Java Tutorial
A Short Course on the Basics
Fifth Edition
Sharon Biocca Zakhour
Sowmya Kannan
Raymond Gallardo
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.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
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.
This document is provided for information purposes only and the contents hereof are subject to change without notice. This document is not warranted to be error-free nor subject to any other warranties or conditions, whether expressed orally or implied in law, including implied warranties and conditions of merchantability or fitness for a particular purpose. We specifically disclaim any liability with respect to this document and no contractual obligations are formed either directly or indirectly by this document. This document may not be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without our prior written permission.
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/aw
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
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-276169-7
ISBN-10: 0-13-276169-6
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan
First printing, March 2013
Contents
Preface
Since the acquisition of Sun Microsystems by Oracle Corporation in early 2010, it has been an exciting time for the Java language. As evidenced by the activities of the Java Community Process program, the Java language continues to evolve. The publication of this fifth edition of The Java Tutorial reflects release 7 of the Java Platform Standard Edition (Java SE) and references the Application Programming Interface (API) of that release.
This edition introduces new features added to the platform since the publication of the fourth edition (under release 6), such as a section on NIO.2, the new file I/O API, and information on migrating legacy code to the new API. The deployment coverage has been expanded with new chapters on ). Information reflecting Project Coin developments has been added where appropriate, including the new try
-with-resources statement, the ability to catch more than one type of exception with a single exception handler, support for binary literals, and diamond syntax, which results in cleaner generics code.
In addition to coverage of new features, previous chapters have been rewritten to include new information. For example, ) have been updated. The appendix for the Java Certification exam has also been completely replaced.
If you plan to take one of the Java SE 7 certification exams, this book can help. The appendix, three exams that are available, detailing the items covered on each exam, cross-referenced to places in the book where you can find more information about each topic. Note that this is one source, among others, that you will want to use to prepare for your exam.
All the material has been thoroughly reviewed by members of Oracle Java engineering to ensure that the information is accurate and up to date. This book is based on the online tutorial hosted on Oracles web site at the following URL:
http://docs.oracle.com/javase/tutorial
The information in this book, often referred to as the core tutorial, is essential for most beginning to intermediate programmers. Once you have mastered this material, you can explore the rest of the Java platform documentation on the web site. If you are interested in developing sophisticated Rich Internet Applications (RIAs), check out JavaFX, the new cutting-edge Java graphical user interface (GUI) toolkit. As of the release of Java SE 7 update 5, you automatically get the JavaFX Software Development Kit (SDK) when you download the JDK. To learn more, see the JavaFX documentation at the following URL:
http://docs.oracle.com/javafx
As always, our goal is to create an easy-to-read, practical programmers guide to help you learn how to use the rich environment provided by Java to build applications, applets, and components. Go forth and program!
Who Should Read This Book?
This book is geared toward both novice and experienced programmers.
New programmers can benefit most from reading the book from beginning to end, including the step-by-step instructions for compiling and running your first program in .
Programmers experienced with procedural languages such as C may want to start with the material on object-oriented concepts and features of the Java programming language.
Experienced programmers may want to jump feet first into the more advanced topics, such as generics, concurrency, or deployment.
This book contains information to address the learning needs of programmers with various levels of experience.
How to Use This Book
This book is designed so you can read it straight through or skip around from topic to topic. The information is presented in a logical order, and forward references are avoided wherever possible.
The examples in this tutorial are compiled against the JDK 7 release. You need to download this release (or later) in order to compile and run most examples.
Some material referenced in this book is available online (e.g., the downloadable examples, the solutions to the questions and exercises, the JDK 7 guides, and the API specification). You will see footnotes like the following:
7/docs/api/java/lang/Class.html
and
tutorial/java/generics/examples/BoxDemo.java
The Java Tutorials are also available in two e-book formats:
mobi e-book files for Kindle
ePub e-book files for iPad, Nook, and other devices that support the ePub format