Starting Out with
Java
Java
TM
From Control Structures
through Objects
This page intentionally left blank
Starting Out with
Java
Java
TM
From Control Structures
through Objects
S i x t h E d i t i O n
Tony Gaddis
Haywood Community College
Boston Columbus
Indianapolis New York San Francisco Hoboken
Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montréal Toronto
Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Editor in Chief: Marcia Horton
Acquisitions Editor: Matt Goldstein
Editorial Assistant: Kelsey Loanes
VP of Marketing: Christy Lesko
Director of Field Marketing: Tim Galligan
Product Marketing Manager: Bram van Kempen
Field Marketing Manager: Demetrius Hall
Marketing Assistant: Jon Bryant
Director of Product Management: Erin Gregg
Team Lead Product Management: Scott Disanno
Program Manager: Carole Snyder
Production Project Manager: Camille Trentacoste
Procurement Manager: Mary Fischer
Senior Specialist, Program Planning and Support:
Maura Zaldivar-Garcia
Cover Designer: Joyce Wells
Cover Image: Binh Thanh Bui/Shutterstock
Manager, Rights Management: Rachel Youdelman
Associate Project Manager, Rights Management:
William J. Opaluch
Full-Service Project Management: Kailash Jadli,
Aptara®, Inc.
Composition: Aptara®, Inc.
Printer/Bindery: Edwards Brothers
Cover printer: Phoenix Color/Hagerstown
Copyright © 2016 by Pearson Education, Inc. or its affiliates. All Rights Reserved. Printed in the United States of America.
This publication is protected by copyright, and permission should 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 otherwise. For information regarding permissions, request forms and the appropriate contacts
within the Pearson Education Global Rights & Permissions department, please visit www.pearsoned.com/permissions/.
Many of the designations 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 in
initial caps or all caps.
Unless otherwise indicated herein, any third-party trademarks that may appear in this work are the property of their
respective owners and any references to third-party trademarks, logos or other trade dress are for demonstrative or
descriptive purposes only. Such references are not intended to imply any sponsorship, endorsement, authorization, or
promotion of Pearson’s products by the owners of such marks, or any relationship between the owner and Pearson
Education, Inc. or its affiliates, authors, licensees or distributors.
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.
Library of Congress Cataloging-in-Publication Data
pages cm
ISBN-13: 978-0-13-395705-1
ISBN-10: 0-13-395705-5
Gaddis, Tony, author.
Starting out with Java. From control structures through objects/Tony Gaddis,
Haywood Community College.—6th edition.
1. Java (Computer program language) 2. Data structures (Computer science)
3. Object-oriented programming (Computer science)
QA76.73.J38G333 2016
005.13'3—dc23
I. Title.
2014049102
10 9 8 7 6 5 4 3 2 1
ISBN-13: 978-0-13-395705-1
ISBN-10:
0-13-395705-5
Contents in Brief
Preface xxiii
Chapter 1
Introduction to Computers and Java
Chapter 2
Java Fundamentals
Chapter 3
Decision Structures
Chapter 4
Loops and Files
Chapter 5 Methods
Chapter 6
A First Look at Classes
Chapter 7
Arrays and the ArrayList Class
Chapter 8
A Second Look at Classes and Objects
Chapter 9
Text Processing and More about Wrapper Classes
Chapter 10
Inheritance
Chapter 11 Exceptions and Advanced File I/O
Chapter 12 A First Look at GUI Applications
Chapter 13 Advanced GUI Applications
Chapter 14 Applets and More
Chapter 15 Creating GUI Applications with JavaFX and Scene Builder
Chapter 16 Recursion
Chapter 17 Databases
Index
Appendixes A–M
Case Studies 1–7
1
27
111
189
269
319
405
495
559
613
703
761
849
917
991
1047
1075
1171
Companion Website
Companion Website
v
This page intentionally left blank
Contents
Preface xxiii
Chapter 1
Chapter 2
1.4
1
Introduction to Computers and Java
1.1
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Computer Systems: Hardware and Software. . . . . . . . . . . . . . . . . . . . . 2
Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
What Is a Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
A History of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Java Applications and Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 What Is a Program Made Of? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Language Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Lines and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
The Compiler and the Java Virtual Machine . . . . . . . . . . . . . . . . . . . . . . .12
Java Software Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Compiling and Running a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . .14
The Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Software Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
1.7 Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Review Questions and Exercises
Programming Challenge
1.6
21
25
27
Java Fundamentals
2.1
2.2
2.3
The Parts of a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The print and println Methods, and the Java API . . . . . . . . . . . . . . . 33
Variables and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Displaying Multiple Items with the + Operator . . . . . . . . . . . . . . . . . . . . .40
Be Careful with Quotation Marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
More about Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42