1. Java Quick Reference Console InputGUI Input DialogScanner
input = new Scanner(System.in); int intValue = input.nextInt();
long longValue = input.nextLong(); double doubleValue =
input.nextDouble(); float floatValue = input.nextFloat(); String
string = input.next();String string = JOptionPane.showInputDialog(
"Enter input"); int intValue = Integer.parseInt(string); double
doubleValue = Double.parseDouble(string); Message DialogConsole
Output JOptionPane.showMessageDialog(null, "Enter
input");System.out.println(anyValue);Primitive Data TypesArithmetic
OperatorsAssignment Operatorsbyte short int long float double char
boolean+ * / % ++var --var var++ var--= += -= *= /= %=8 bits 16
bits 32 bits 64 bits 32 bits 64 bits 16 bits true/falseaddition
subtraction multiplication division remainder preincrement
predecrement postincrement postdecrementassignment addition
assignment subtraction assignment multiplication assignment
division assignment remainder assignmentRelational OperatorsLogical
Operatorsif Statements< >= == !=&& || ! ^if
(condition) { statements; }less than less than or equal to greater
than greater than or equal to equal to not equalshort circuit AND
short circuit OR NOT exclusive ORswitch Statementsloop
Statementsswitch (intExpression) { case value1: statements; break;
... case valuen: statements; break; default: statements; }while
(condition) { statements; } do { statements; } while (condition);
for (init; condition; adjustment) { statements; }if (condition) {
statements; } else { statements; } if (condition1) { statements; }
else if (condition2) { statements; } else { statements; }Companion
Web site: www.pearsonhighered.com/liang
2. Java Quick Reference Frequently Used Static
Constants/Methods Math.PI Math.random() Math.pow(a, b)
System.currentTimeMillis() System.out.println(anyValue)
JOptionPane.showMessageDialog(null, message)
JOptionPane.showInputDialog( prompt-message)
Integer.parseInt(string) Double.parseDouble(string)
Arrays.sort(type[] list) Arrays.binarySearch(type[] list, type
key)Array/Length/Initializer int[] list = new int[10]; list.length;
int[] list = {1, 2, 3, 4}; Multidimensional
Array/Length/Initializer int[][] list = new int[10][10];
list.length; list[0].length; int[][] list = {{1, 2}, {3, 4}};
Ragged Array int[][] m = {{1, 2, 3, 4}, {1, 2, 3}, {1, 2},
{1}};Text File OutputFile ClassPrintWriter output = new
PrintWriter(filename); output.print(...); output.println(...);
output.printf(...);Object ClassFile file = new File(filename);
file.exists() file.renameTo(File) file.delete()Object o = new
Object(); o.toString(); o.equals(o1);Comparable InterfaceText File
Inputc.compareTo(Comparable) c is a Comparable objectScanner input
= new Scanner( new File(filename));String ClassArrayList
ClassString s = "Welcome"; String s = new String(char[]); int
length = s.length(); char ch = s.charAt(index); int d =
s.compareTo(s1); boolean b = s.equals(s1); boolean b =
s.startsWith(s1); boolean b = s.endsWith(s1); String s1 = s.trim();
String s1 = s.toUpperCase(); String s1 = s.toLowerCase(); int index
= s.indexOf(ch); int index = s.lastIndexOf(ch); String s1 =
s.substring(ch); String s1 = s.substring(i,j); char[] chs =
s.toCharArray(); String s1 = s.replaceAll(regex,repl); String[]
tokens = s.split(regex);ArrayList list = new ArrayList();
list.add(object); list.add(index, object); list.clear(); Object o =
list.get(index); boolean b = list.isEmpty(); boolean b =
list.contains(object); int i = list.size(); list.remove(index);
list.set(index, object); int i = list.indexOf(object); int i =
list.lastIndexOf(object);printf Method System.out.printf("%b %c %d
%f %e %s", true, 'A', 45, 45.5, 45.5, "Welcome");
System.out.printf("%-5d %10.2f %10.2e %8s", 45, 45.5, 45.5,
"Welcome");Companion Web site: www.pearsonhighered.com/liang
3. INTRODUCTION TOJAVA TMPROGRAMMING COMPREHENSIVE VERSION
Eighth EditionY. Daniel Liang Armstrong Atlantic State
UniversityPrentice Hall Boston Columbus Indianapolis New York San
Francisco Upper Saddle River Amsterdam Cape Town Dubai London
Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao
Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
4. Vice President and Editorial Director, ECS: Marcia J. Horton
Editor in Chief, Computer Science: Michael Hirsch Executive Editor:
Tracy Dunkelberger Assistant Editor: Melinda Haggerty Editorial
Assistant: Allison Michael Vice President, Production: Vince OBrien
Senior Managing Editor: Scott Disanno Production Editor: Irwin
Zucker Senior Operations Specialist: Alan Fischer Marketing
Manager: Erin Davis Marketing Assistant: Mack Patterson Art
Director: Kenny Beck Cover Image: Male Ruby-throated Hummingbird /
Steve Byland / Shutterstock; Hummingbird, Nazca Lines / Gary Yim /
Shutterstock Art Editor: Greg Dulles Media Editor: Daniel
SandinCopyright 2011, 2009, 2007, 2004 by Pearson Higher Education.
Upper Saddle River, New Jersey, 07458. All right reserved.
Manufactured 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 likewise. To
obtain permission(s) to use materials from this work, please submit
a written request to Pearson Higher Education, Permissions
Department, 1 Lake Street, Upper Saddle River, NJ 07458. The author
and publisher of this book have used their best efforts in
preparing this book. These efforts include the development,
research, and testing of the theories and programs to determine
their effectiveness. The author and publisher make no warranty of
any kind, expressed or implied, with regard to these programs or
the documentation contained in this book. The author and publisher
shall not be liable in any event for incidental or consequential
damages in connection with, or arising out of, the furnishing,
performance, or use of these programs.Library of Congress
Cataloging-in-Publication Data on file.10 9 8 7 6 5 4 3 2 1
ISBN-13: 978-0-13-213080-6 ISBN-10: 0-13-213080-7
5. This book is dedicated to Dr. S. K. Dhall and Dr. S.
Lakshmivarahan of the University of Oklahoma, who inspired me in
teaching and research. Thank you for being my mentors and
advisors.To Samantha, Michael, and Michelle
6. PREFACE fundamentals-firstproblem-drivenThis book uses the
fundamentals-first approach and teaches programming concepts and
techniques in a problem-driven way. The fundamentals-first approach
introduces basic programming concepts and techniques before objects
and classes. My own experience, confirmed by the experiences of
many colleagues, demonstrates that new programmers in order to
succeed must learn basic logic and fundamental programming
techniques such as loops and stepwise refinement. The fundamental
concepts and techniques of loops, methods, and arrays are the
foundation for programming. Building the foundation prepares
students to learn object-oriented programming, GUI, database, and
Web programming. Problem-driven means focused on problem solving
rather than syntax. We make introductory programming interesting by
using interesting problems. The central thread of early chapters is
on problem solving. Appropriate syntax and library are introduced
to support the writing of a program for solving the problems. To
support the teaching of programming in a problemdriven way, the
book provides a wide variety of problems at various levels of
difficulty to motivate students. In order to appeal to students in
all majors, the problems cover many application areas in math,
science, business, financials, gaming, animation, and
multimedia.Two Versions comprehensive versionbrief versionThis
comprehensive version covers fundamentals of programming,
object-oriented programming, GUI programming, algorithms and data
structures, concurrency, networking, internationalization, advanced
GUI, database, and Web programming. It is designed to prepare
students to become proficient Java programmers. A brief version
(Introduction to Java Programming, Brief Version, Eighth Edition)
is available for a first course on programming, commonly known as
CS1. The brief version contains the first 20 chapters of the
comprehensive version.Whats New in This Edition? This edition
substantially improves Introduction to Java Programming, Seventh
Edition. The major improvements are as follows: This edition is
completely revised in every detail to enhance clarity,
presentation, content, examples, and exercises.In the examples and
exercises, which are provided to motivate and stimulate student
interest in programming, one-fifth of the problems are new.early
console inputIn the previous edition, console input was covered at
the end of Chapter 2. The new edition introduces console input
early in Chapter 2 so that students can write interactive programs
early.hand trace boxThe hand trace box is added for many programs
to help novice students to read and trace programs.multidimensional
arraysSingle-dimensional arrays and multidimensional arrays are
covered in two chapters to give instructors the flexibility to
cover multidimensional arrays later.Sudoku problem simplifiedThe
case study for the Sudoku problem has been moved to the Companion
Website. A more pedagogically effective simple version of the
Sudoku problem is presented instead.The design of the API for Java
GUI programming is an excellent example of how the object-oriented
principle is applied. Students learn better with concrete and
visual examples.complete revisionnew problemsiv
7. Preface v So, basic GUI now precedes the introduction of
abstract classes and interfaces. The instructor, however, can still
choose to cover abstract classes and interfaces before GUI.basic
GUI earlierException handling is covered before abstract classes
and interfaces so that students can build robust programs early.
The instructor can still choose to cover exception handling
later.exception handling earlierChapter 12, Object-Oriented Design
and Patterns, in the previous edition has been replaced by
spreading the design guidelines and patterns into several chapters
so that these topics can be covered in appropriate context.design
guidelinesThe chapter on sorting now follows right after the
chapter on algorithm efficiency, so that students can immediately
apply algorithm efficiency to sorting algorithms.sortingA brand-new
bonus Chapter 44 covers Java 2D.Java 2DThe coverage on data
structures is expanded with new bonus chapters on AVL trees, splay
trees, 2-4 trees, B-trees, and red-black trees, and hashing. So the
book can be used for a full data structures course.new data
structures chaptersLearning Strategies A programming course is
quite different from other courses. In a programming course, you
learn from examples, from practice, and from mistakes. You need to
devote a lot of time to writing programs, testing them, and fixing
errors. For first-time programmers, learning Java is like learning
any high-level programming language. The fundamental point is to
develop the critical skills of formulating programmatic solutions
for real problems and translating them into programs using
selection statements, loops, methods, and arrays. Once you acquire
the basic skills of writing programs using loops, methods, and
arrays, you can begin to learn how to develop large programs and
GUI programs using the objectoriented approach. When you know how
to program and you understand the concept of object-oriented
programming, learning Java becomes a matter of learning the Java
API. The Java API establishes a framework for programmers to
develop applications using Java. You have to use the classes and
interfaces in the API and follow their conventions and rules to
create applications. The best way to learn the Java API is to
imitate examples and do exercises.Pedagogical Features The book
uses the following elements to get the most from the material:
Objectives list what students should have learned from the chapter.
This will help them determine whether they have met the objectives
after completing the chapter.Introduction opens the discussion with
representative problems to give the reader an overview of what to
expect from the chapter.Problems carefully chosen and presented in
an easy-to-follow style, teach problem solving and programming
concepts. The book uses many small, simple, and stimulating
examples to demonstrate important ideas.Chapter Summary reviews the
important subjects that students should understand and remember. It
helps them reinforce the key concepts they have learned in the
chapter.Review Questions are grouped by sections to help students
track their progress and evaluate their learning.Programming
Exercises are grouped by sections to provide students with
opportunities to apply on their own the new skills they have
learned. The level of difficulty is rated as easy (nolearn from
mistakesprogrammatic solutionobject-oriented programmingJava
API
8. vi Preface asterisk), moderate (*), hard (**), or
challenging (***). The trick of learning programming is practice,
practice, and practice. To that end, the book provides a great many
exercises. LiveLab is a course assessment and management system.
Students can submit programs online. The system automatically
grades the programs/multiple-choice quizzes and gives students
instant feedback. Instructors can create custom programming
exercises and quizzes as well as use the system prebuilt exercises
and quizzes.Notes, Tips, and Cautions are inserted throughout the
text to offer valuable advice and insight on important aspects of
program development.Note Provides additional information on the
subject and reinforces important concepts.Tip Teaches good
programming style and practice.Caution Helps students steer away
from the pitfalls of programming errors.Design Guide Provides the
guidelines for designing programs.Flexible Chapter Orderings The
book is designed to provide flexible chapter orderings to enable
GUI, exception handling, recursion, generics, and the Java
Collections Framework to be covered earlier or later. The diagram
on the next page shows the chapter dependencies.Organization of the
Book The chapters can be grouped into five parts that, taken
together, form a comprehensive introduction to Java programming,
data structures and algorithms, and database and Web programming.
Because knowledge is cumulative, the early chapters provide the
conceptual basis for understanding programming and guide students
through simple examples and exercises; subsequent chapters
progressively present Java programming in detail, culminating with
the development of comprehensive Java applications. Part I:
Fundamentals of Programming (Chapters 17) The first part of the
book is a stepping stone, preparing you to embark on the journey of
learning Java. You will begin to know Java (Chapter 1) and will
learn fundamental programming techniques with primitive data types,
variables, constants, assignments, expressions, and operators
(Chapter 2), control statements (Chapters 34), methods (Chapter 5),
and arrays (Chapters 67). After Chapter 6, you may jump to Chapter
20 to learn how to write recursive methods for solving inherently
recursive problems. Part II: Object-Oriented Programming (Chapters
811, 1314, 19) This part introduces object-oriented programming.
Java is an object-oriented programming language that uses
abstraction, encapsulation, inheritance, and polymorphism to
provide great flexibility, modularity, and reusability in
developing software. You will learn programming with objects and
classes (Chapters 810), class inheritance (Chapter 11),
polymorphism (Chapter 11), exception handling (Chapter 13),
abstract classes (Chapter 14), and interfaces (Chapter 14).
Processing strings will be introduced in Chapter 9 along with text
I/O. Binary I/O is introduced in Chapter 19.
9. Part I: Fundamentals of ProgrammingPart II: Object-Oriented
ProgrammingPart III: GUI ProgrammingPart IV: Data Structures and
AlgorithmsCh 18Part V: Advanced Java ProgrammingChapter 8 Objects
and ClassesChapter 12 GUI BasicsCh 6Chapter 20 RecursionChapter 29
MultithreadingChapter 9 Strings and Text I/OChapter 15 GraphicsCh
14Chapter 21 GenericsChapter 30 NetworkingChapter 2 Elementary
ProgrammingChapter 10 Thinking in ObjectsChapter 16 Event-Driven
ProgrammingChapter 22 Java Collections FrameworkChapter 31
InternationalizationChapter 3 SelectionsChapter 11 Inheritance and
PolymorphismChapter 17 Creating Graphical User InterfacesChapter 23
Algorithm EfficiencyChapter 1 Introduction to Computers, Programs,
and JavaChapter 4 LoopsChapter 13 Exception HandlingChapter 5
Methods Chapter 6 Single-Dimensional ArraysChapter 14 Abstract
Classes and InterfacesChapter 24 Sorting Chapter 18 Applets and
Multimedia Chapter 32 JavaBeans and Bean Events Chapter 33
Containers, Layout Managers, and Borders Chapter 34 Menus,
Toolbars, and Dialogs Chapter 35 MVC and Swing ModelsChapter 38
Advanced Java Database ProgrammingChapter 25 Lists, Stacks, and
Queues, and Priority QueuesChapter 39 ServletsChapter 26 Binary
Search TreesChapter 40 JavaServer PagesChapter 27 Graphs and
ApplicationsChapter 41 JSF and Visual Web DevelopmentChapter 28
Weighted Graphs and ApplicationsChapter 42 Web ServicesChapter 19
Binary I/O Chapter 7 Multidimensional ArraysChapter 37 Java
Database ProgrammingChapter 45 AVL and Splay TreesChapter 43 Remote
Method InvocationChapter 36 JTable and JTree Note: Chapters 120 are
in the brief version of this book Note: Chapters 3848 are bonus
chapters available from the Companion WebsiteChapter 46 2-4 Trees
and B-Trees Chapter 44 Java 2D Chapter 47 Red-Black Trees Chapter
48 HashingPreface vii
10. viii Preface Part III: GUI Programming (Chapters 12, 1518,
3236, and 44) This part introduces elementary Java GUI programming
in Chapters 12 and 1518 and advanced Java GUI programming in
Chapters 3236 and 44. Major topics include GUI basics (Chapter 12),
drawing shapes (Chapter 15), event-driven programming (Chapter 16),
creating graphical user interfaces (Chapter 17), and writing
applets (Chapter 18). You will learn the architecture of Java GUI
programming and use the GUI components to develop applications and
applets from these elementary GUI chapters. The advanced GUI
chapters introduce Java GUI programming in more depth and breadth.
You will delve into JavaBeans and learn how to develop custom
events and source components in Chapter 32, review and explore new
containers, layout managers, and borders in Chapter 33, learn how
to create GUI with menus, popup menus, toolbars, dialogs, and
internal frames in Chapter 34, develop components using the MVC
approach and explore the advanced Swing components JSpinner, JList,
JComboBox, JSpinner, and JTable, and JTree in Chapters 35 and 36.
Bonus Chapter 44 introduces Java 2D. Part IV: Algorithms and Data
Structures (Chapters 2028, 4548) This part introduces the main
subjects in a typical data structures course. Chapter 20 introduces
recursion to write methods for solving inherently recursive
problems. Chapter 21 introduces generics to improve software
reliability. Chapter 22 introduces the Java Collection Framework,
which defines a set of useful API for data structures. Chapter 23
introduces measurement of algorithm efficiency in order to choose
an appropriate algorithm for applications. Chapter 24 introduces
classic sorting algorithms. You will learn how to implement several
classic data structures lists, queues, priority queues, binary
search trees, AVL trees, splay trees, 2-4 trees, B-trees, and
red-black trees in Chapters 2526 and 4547. Chapters 27 and 28
introduce graph applications. Chapter 48 introduces hashing. Part
V: Advanced Java Programming (Chapters 2931, 3743) This part of the
book is devoted to advanced Java programming. Chapter 29 treats the
use of multithreading to make programs more responsive and
interactive. Chapter 30 introduces how to write programs that talk
with each other from different hosts over the Internet. Chapter 31
covers the use of internationalization support to develop projects
for international audiences. Chapter 37 introduces the use of Java
to develop database projects, Chapter 38 introduces advanced Java
database programming, and Chapters 39 and 40 introduce how to use
Java servlets and JSP to generate dynamic contents from Web
servers. Chapter 41 introduces rapid Web application development
using JavaServer Faces. Chapter 42 introduces Web services. Chapter
43 introduces remote method invocation.Java Development Tools IDE
tutorialsYou can use a text editor, such as the Windows Notepad or
WordPad, to create Java programs and to compile and run the
programs from the command window. You can also use a Java
development tool, such as TextPad, NetBeans, or Eclipse. These
tools support an integrated development environment (IDE) for
rapidly developing Java programs. Editing, compiling, building,
executing, and debugging programs are integrated in one graphical
user interface. Using these tools effectively can greatly increase
your programming productivity. TextPad is a primitive IDE tool.
NetBeans and Eclipse are more sophisticated, but they are easy to
use if you follow the tutorials. Tutorials on TextPad, NetBeans and
Eclipse can be found in the supplements on the Companion
Website.LiveLab This book is accompanied by an improved faster
Web-based course assessment and management system. The system has
three main components:
11. Preface ix Automatic Grading System: It can automatically
grade programs from the text or created by instructors.Quiz
Creation/Submission/Grading System: It enables instructors to
create/modify quizzes that students can take and be graded upon
automatically.Tracking grades, attendance, etc: The system enables
the students to track grades and instructors, to view the grades of
all students, and to track attendance.The main features of the
Automatic Grading System are as follows: Allows students to
compile, run and submit exercises. (The system checks whether their
program runs correctlystudents can continue to run and resubmit the
program before the due date.)Allows instructors to review
submissions; run programs with instructor test cases; correct them;
and provide feedback to students.Allows instructors to
create/modify custom exercises, create public and secret test
cases, assign exercises, and set due dates for the whole class or
for individuals.All the exercises in the text can be assigned to
students. Additionally, LiveLab provides extra exercises that are
not printed in the text.Allows instructors to sort and filter all
exercises and check grades (by time frame, student, and/or
exercise).Allows instructors to delete students from the
system.Allows students and instructors to track grades on
exercises.The main features of the Quiz System are as follows:
Allows instructors to create/modify quizzes from test bank or a
text file or to create complete new tests online.Allows instructors
to assign the quizzes to students and set a due date and test time
limit for the whole class or for individuals.Allows students and
instructors to review submitted quizzes.Allows students and
instructors to track grades on quizzes.Video Notes are Pearsons new
visual tool designed for teaching students key programming concepts
and techniques. These short step-by-step videos demonstrate how to
solve problems from design through coding. Video Notes allows for
self-paced instruction with easy navigation including the ability
to select, play, rewind, fast-forward, and stop within each Video
Note exercise. Video Note margin icons in your textbook let you
know what a Video Notes video is available for a particular concept
or homework problem. Video Notes are free with the purchase of a
new textbook. To purchase access to Video Notes, please go to
www.pearsonhighered.com/liang.Student Resource Materials The
student resources can be accessed through the Publishers Web site
(www.pearsonhighered.com/liang) and the Companion Web site
(www.cs.armstrong.edu/liang/intro8e). The resources include:
Answers to review questionsSolutions to even-numbered programming
exercises
12. x Preface Source code for book examplesInteractive
self-test (organized by chapter sections)LiveLabResource
linksErrataVideo NotesWeb ChaptersTo access the Video Notes and Web
Chapters, students must log onto www.pearsonhighered.com/liang and
use the access card located in the front of the book to register
and access the material. If there is no access card in the front of
this textbook, students can purchase access by visiting
www.pearsonhighered.com/liang and selecting purchase access to
premium content.Additional Supplements The text covers the
essential subjects. The supplements extend the text to introduce
additional topics that might be of interest to readers. The
supplements listed in this table are available from the Companion
Web site.Supplements on the Companion Web site Part I General
Supplements A Glossary B Installing and Configuring JDK C Compiling
and Running Java from the Command Window D Java Coding Style
Guidelines E Creating Desktop Shortcuts for Java Applications on
Windows F Using Packages to Organize the Classes in the Text Part
II IDE Supplements A TextPad Tutorial B NetBeans Tutorial | One
Page Startup Instruction C Learning Java Effectively with NetBeans
D Eclipse Tutorial | One Page Startup Instruction E Learning Java
Effectively with Eclipse Part III Java Supplements A Java
Characteristics B Discussion on Operator and Operand Evaluations C
The & and | Operators D Bitwise Operations E Statement Labels
with break and continueF G H I J KEnumerated Types Packages Regular
Expressions Formatted Strings The Methods in the Object Class
Hiding Data Fields and Static Methods L Initialization Blocks M
Extended Discussions on Overriding Methods N Design Patterns O Text
I/O Prior to JDK 1.5 (Reader and Writer Classes) P Assertions Q
Packaging and Deploying Java Projects R Java Web Start S
GridBagLayout | OverlayLayout | SpringLayout T Networking Using
Datagram Protocol U Creating Internal Frames V Pluggable Look and
Feel W UML Graphical Notations X Testing Classes Using JUnit Y JNI
Z The StringTokenizer Class Part IV Database Supplements A SQL
Statements for Creating and Initializing Tables Used in the
Book
13. Preface xi B C D E F G H IMySQL Tutorial Oracle Tutorial
Microsoft Access Tutorial Introduction to Database Systems
Relational Database Concept Database Design SQL Basics Advanced
SQLPart V Web Programming Supplements A HTML and XHTML Tutorial B
CSS Tutorial C XML D Java and XML E Tomcat Tutorial F More Examples
on JSF and Visual Web DevelopmentInstructor Resource Materials The
instructor resources can be accessed through the Publishers Web
site (www.pearsonhighered.com/liang) and the Companion Web site
(www.cs.armstrong.edu/liang/intro8e). For username and password
information to the Liang 8e site, please contact your Pearson
Representative. The resources include: PowerPoint lecture slides
with source code and run program capacityInstructor solutions
manualComputerized test generatorSample exams using multiple choice
and short answer questions, write and trace programs, and
correcting programming errors.LiveLabErrataVideo NotesWeb
ChaptersTo access the Video Notes and Web Chapters, instructors
must log onto www.pearsonhighered.com/liang and
register.Acknowledgments I would like to thank Armstrong Atlantic
State University for enabling me to teach what I write and for
supporting me in writing what I teach. Teaching is the source of
inspiration for continuing to improve the book. I am grateful to
the instructors and students who have offered comments,
suggestions, bug reports, and praise. This book has been greatly
enhanced thanks to outstanding reviews for this and previous
editions. The reviewers are: Elizabeth Adams (James Madison
University), Syed Ahmed (North Georgia College and State
University), Omar Aldawud (Illinois Institute of Technology), Yang
Ang (University of Wollongong, Australia), Kevin Bierre (Rochester
Institute of Technology), David Champion (DeVry Institute), James
Chegwidden (Tarrant County College), Anup Dargar (University of
North Dakota), Charles Dierbach (Towson University), Frank Ducrest
(University of Louisiana at Lafayette), Erica Eddy (University of
Wisconsin at Parkside), Deena Engel (New York University), Henry A
Etlinger (Rochester Institute of Technology), James Ten Eyck
(Marist College), Olac Fuentes (University of Texas at El Paso),
Harold Grossman (Clemson University), Barbara Guillot (Louisiana
State University), Ron Hofman (Red River College, Canada), Stephen
Hughes (Roanoke College), Vladan Jovanovic (Georgia Southern
University), Edwin Kay (Lehigh University), Larry King (University
of Texas at Dallas), Nana Kofi (Langara College, Canada), George
Koutsogiannakis (Illinois
14. xii Preface Institute of Technology), Roger Kraft (Purdue
University at Calumet), Hong Lin (DeVry Institute), Dan Lipsa
(Armstrong Atlantic State University), James Madison (Rensselaer
Polytechnic Institute), Frank Malinowski (Darton College), Tim
Margush (University of Akron), Debbie Masada (Sun Microsystems),
Blayne Mayfield (Oklahoma State University), John McGrath (J.P.
McGrath Consulting), Shyamal Mitra (University of Texas at Austin),
Michel Mitri (James Madison University), Kenrick Mock (University
of Alaska Anchorage), Jun Ni (University of Iowa), Benjamin Nystuen
(University of Colorado at Colorado Springs), Maureen Opkins (CA
State University, Long Beach), Gavin Osborne (University of
Saskatchewan), Kevin Parker (Idaho State University), Dale Parson
(Kutztown University), Mark Pendergast (Florida Gulf Coast
University), Richard Povinelli (Marquette University), Roger Priebe
(University of Texas at Austin), Mary Ann Pumphrey (De Anza Junior
College), Pat Roth (Southern Polytechnic State University), Ronald
F. Taylor (Wright State University), Carolyn Schauble (Colorado
State University), David Scuse (University of Manitoba), Ashraf
Shirani (San Jose State University), Daniel Spiegel (Kutztown
University), Amr Sabry (Indiana University), Lixin Tao (Pace
University), Russ Tront (Simon Fraser University), Deborah Trytten
(University of Oklahoma), Kent Vidrine (George Washington
University), and Bahram Zartoshty (California State University at
Northridge). It is a great pleasure, honor, and privilege to work
with Pearson. I would like to thank Tracy Dunkelberger and her
colleagues Marcia Horton, Margaret Waples, Erin Davis, Michael
Hirsh, Matt Goldstein, Jake Warde, Melinda Haggerty, Allison
Michael, Scott Disanno, Irwin Zucker, and their colleagues for
organizing, producing, and promoting this project, and Robert Lentz
for copy editing. As always, I am indebted to my wife, Samantha,
for her love, support, and encouragement. Y. Daniel Liang
[email protected] www.cs.armstrong.edu/liang
www.pearsonhighered.com/liang
15. BRIEF CONTENTS 1 Introduction to Computers, Programs, 2 3 4
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31and Java Elementary Programming Selections Loops Methods
Single-Dimensional Arrays Multidimensional Arrays Objects and
Classes Strings and Text I/O Thinking in Objects Inheritance and
Polymorphism GUI Basics Exception Handling Abstract Classes and
Interfaces Graphics Event-Driven Programming Creating Graphical
User Interfaces Applets and Multimedia Binary I/O Recursion
Generics Java Collections Framework Algorithm Efficiency Sorting
Lists, Stacks, Queues, and Priority Queues Binary Search Trees
Graphs and Applications Weighted Graphs and Applications
Multithreading Networking Internationalization1 23 71 115 155 197
235 263 301 343 373 405 431 457 497 533 571 613 649 677 707 727 765
791 821 857 891 939 971 1017 105732 JavaBeans and Bean Events 33
Containers, Layout Managers,1091and Borders Menus, Toolbars, and
Dialogs MVC and Swing Models JTable and JTree Java Database
Programming1111 1149 1187 1225 127334 35 36 37Chapters are
available from the companion Web site at
www.pearsonhighered.com/liang: 38 39 40 41 42 43 44 45 46 47
48Advanced Java Database Programming Servlets JavaServer Pages JSF
and Visual Web Development Web Services Remote Method Invocation
Java 2D AVL Trees and Splay Trees 2-4 Trees and B-Trees Red-Black
Trees Hashing381 391 401 411 421 431 441 451 461 471 481APPENDIXES
A B C D E FJava Keywords The ASCII Character Set Operator
Precedence Chart Java Modifiers Special Floating-Point Values
Number SystemsINDEX1309 1312 1314 1316 1318 1319 1323xiii
16. CONTENTS Chapter 1 Introduction to Computers, Programs, and
Java 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9Introduction What Is a
Computer? Programs Operating Systems Java, World Wide Web, and
Beyond The Java Language Specification, API, JDK, and IDE A Simple
Java Program Creating, Compiling, and Executing a Java Program
(GUI) Displaying Text in a Message Dialog BoxChapter 2 Elementary
Programming 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12 2.13
2.14 2.15 2.16 2.17 2.18Introduction Writing Simple Programs
Reading Input from the Console Identifiers Variables Assignment
Statements and Assignment Expressions Named Constants Numeric Data
Types and Operations Problem: Displaying the Current Time Shorthand
Operators Numeric Type Conversions Problem: Computing Loan Payments
Character Data Type and Operations Problem: Counting Monetary Units
The String Type Programming Style and Documentation Programming
Errors (GUI) Getting Input from Input DialogsChapter 3 Selections
3.1 3.2 3.3 3.4 3.5 3.6 3.7xivIntroduction boolean Data Type
Problem: A Simple Math Learning Tool if Statements Problem:
Guessing Birthdays Two-Way if Statements Nested if Statements1 2 2
5 7 8 10 11 13 16 23 24 24 26 29 29 30 31 32 37 39 41 43 44 47 50
51 53 55 71 72 72 73 74 75 79 80
17. Contents xv 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17
3.18 3.19Common Errors in Selection Statements Problem: An Improved
Math Learning Tool Problem: Computing Body Mass Index Problem:
Computing Taxes Logical Operators Problem: Determining Leap Year
Problem: Lottery switch Statements Conditional Expressions
Formatting Console Output Operator Precedence and Associativity
(GUI) Confirmation Dialogs81 82 84 85 88 90 91 93 95 95 97
98Chapter 4 Loops1154.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10116 116
124 126 128 129 130 131 135 139Introduction The while Loop The
do-while Loop The for Loop Which Loop to Use? Nested Loops
Minimizing Numeric Errors Case Studies Keywords break and continue
(GUI) Controlling a Loop with a Confirmation DialogChapter 5
Methods 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11
5.12Introduction Defining a Method Calling a Method void Method
Example Passing Parameters by Values Modularizing Code Problem:
Converting Decimals to Hexadecimals Overloading Methods The Scope
of Variables The Math Class Case Study: Generating Random
Characters Method Abstraction and Stepwise RefinementChapter 6
Single-Dimensional Arrays 6.1 6.2 6.3 6.4 6.5Introduction Array
Basics Problem: Lotto Numbers Problem: Deck of Cards Copying
Arrays155 156 156 158 160 162 165 167 168 171 172 175 176 197 198
198 204 206 208
18. xvi Contents 6.6 6.7 6.8 6.9 6.10 6.11Passing Arrays to
Methods Returning an Array from a Method Variable-Length Argument
Lists Searching Arrays Sorting Arrays The Arrays ClassChapter 7
Multidimensional Arrays 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8Introduction
Two-Dimensional Array Basics Processing Two-Dimensional Arrays
Passing Two-Dimensional Arrays to Methods Problem: Grading a
Multiple-Choice Test Problem: Finding a Closest Pair Problem:
Sudoku Multidimensional ArraysChapter 8 Objects and Classes 8.1 8.2
8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 8.11Introduction Defining Classes
for Objects Example: Defining Classes and Creating Objects
Constructing Objects Using Constructors Accessing Objects via
Reference Variables Using Classes from the Java Library Static
Variables, Constants, and Methods Visibility Modifiers Data Field
Encapsulation Passing Objects to Methods Array of ObjectsChapter 9
Strings and Text I/O 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8Introduction
The String Class The Character Class The StringBuilder/StringBuffer
Class Command-Line Arguments The File Class File Input and Output
(GUI) File DialogsChapter 10 Thinking in Objects 10.1 10.2 10.3
10.4 10.5Introduction Immutable Objects and Classes The Scope of
Variables The this Reference Class Abstraction and Encapsulation209
212 215 216 219 223 235 236 236 238 240 241 242 244 248 263 264 264
266 270 270 274 278 282 283 286 287 301 302 302 313 315 320 322 325
329 343 344 344 345 346 347
19. Contents xvii 10.6 10.7 10.8 10.9 10.10
10.11Object-Oriented Thinking Object Composition Designing the
Course Class Designing a Class for Stacks Designing the GuessDate
Class Class Design GuidelinesChapter 11 Inheritance and
Polymorphism 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10
11.11 11.12 11.13 11.14Introduction Superclasses and Subclasses
Using the super Keyword Overriding Methods Overriding vs.
Overloading The Object Class and Its toString() Method Polymorphism
Dynamic Binding Casting Objects and the instanceof Operator The
Objects equals() Method The ArrayList Class A Custom Stack Class
The protected Data and Methods Preventing Extending and
OverridingChapter 12 GUI Basics 12.1 12.2 12.3 12.4 12.5 12.6 12.7
12.8 12.9 12.10Introduction Swing vs. AWT The Java GUI API Frames
Layout Managers Using Panels as Subcontainers The Color Class The
Font Class Common Features of Swing GUI Components Image
IconsChapter 13 Exception Handling 13.1 13.2 13.3 13.4 13.5 13.6
13.7 13.8 13.9 13.10Introduction Exception-Handling Overview
Exception-Handling Advantages Exception Types More on Exception
Handling The finally Clause When to Use Exceptions Rethrowing
Exceptions Chained Exceptions Creating Custom Exception Classes351
353 355 357 359 362 373 374 374 380 382 383 384 384 385 387 389 390
393 394 396 405 406 406 406 408 411 417 419 419 420 422 431 432 432
434 437 439 445 447 447 447 448
20. xviii ContentsChapter 14 Abstract Classes and Interfaces
14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 14.9 14.10 14.11 14.12
14.13Introduction Abstract Classes Example: Calendar and
GregorianCalendar Interfaces Example: The Comparable Interface
Example: The ActionListener Interface Example: The Cloneable
Interface Interfaces vs. Abstract Classes Processing Primitive Data
Type Values as Objects Sorting an Array of Objects Automatic
Conversion between Primitive Types and Wrapper Class Types The
BigInteger and BigDecimal Classes Case Study: The Rational
ClassChapter 15 Graphics 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8
15.9 15.10 15.11 15.12Introduction Graphical Coordinate Systems The
Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Case
Study: The FigurePanel Class Drawing Arcs Drawing Polygons and
Polylines Centering a String Using the FontMetrics Class Case
Study: The MessagePanel Class Case Study: The StillClock Class
Displaying Images Case Study: The ImageViewer ClassChapter 16
Event-Driven Programming 16.1 16.2 16.3 16.4 16.5 16.6 16.7 16.8
16.9 16.10 16.11 16.12Introduction Event and Event Source
Listeners, Registrations, and Handling Events Inner Classes
Anonymous Class Listeners Alternative Ways of Defining Listener
Classes Problem: Loan Calculator Window Events Listener Interface
Adapters Mouse Events Key Events Animation Using the Timer
ClassChapter 17 Creating Graphical User Interfaces 17.1 17.2
17.3Introduction Buttons Check Boxes457 458 458 462 465 467 469 471
473 476 479 481 481 482 497 498 498 499 501 502 506 507 510 512 516
520 522 533 534 534 535 541 542 544 547 549 551 552 555 557 571 572
572 578
21. Contents xix 17.4 17.5 17.6 17.7 17.8 17.9 17.10 17.11
17.12Radio Buttons Labels Text Fields Text Areas Combo Boxes Lists
Scroll Bars Sliders Creating Multiple WindowsChapter 18 Applets and
Multimedia 18.1 18.2 18.3 18.4 18.5 18.6 18.7 18.8 18.9 18.10 18.11
18.12Introduction Developing Applets The HTML File and the Tag
Applet Security Restrictions Enabling Applets to Run as
Applications Applet Life-Cycle Methods Passing Strings to Applets
Case Study: Bouncing Ball Case Study: TicTacToe Locating Resources
Using the URL Class Playing Audio in Any Java Program Case Study:
Multimedia AnimationsChapter 19 Binary I/O 19.1 19.2 19.3 19.4 19.5
19.6 19.7Introduction How is I/O Handled in Java? Text I/O vs.
Binary I/O Binary I/O Classes Problem: Copying Files Object I/O
Random-Access FilesChapter 20 Recursion 20.1 20.2 20.3 20.4 20.5
20.6 20.7 20.8 20.9 20.10 20.11Introduction Problem: Computing
Factorials Problem: Computing Fibonacci Numbers Problem Solving
Using Recursion Recursive Helper Methods Problem: Finding the
Directory Size Problem: Towers of Hanoi Problem: Fractals Problem:
Eight Queens Recursion vs. Iteration Tail Recursion581 583 584 586
590 593 596 599 602 613 614 614 615 618 618 620 620 624 628 632 633
634 649 650 650 650 652 660 662 666 677 678 678 681 683 684 687 688
692 695 697 697
22. xx ContentsChapter 21 Generics 21.1 21.2 21.3 21.4 21.5
21.6 21.7 21.8Introduction Motivations and Benefits Defining
Generic Classes and Interfaces Generic Methods Raw Type and
Backward Compatibility Wildcard Generic Types Erasure and
Restrictions on Generics Case Study: Generic Matrix ClassChapter 22
Java Collections Framework 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8
22.9 22.10 22.11 22.12Introduction Collections The Collection
Interface and the AbstractCollection Class Sets The Comparator
Interface Lists Static Methods for Lists and Collections
Performance of Sets and Lists The Vector and Stack Classes Queues
and Priority Queues Maps Singleton and Unmodifiable Collections and
MapsChapter 23 Algorithm Efficiency 23.1 23.2 23.3 23.4 23.5 23.6
23.7 23.8 23.9Introduction Big O Notation Examples: Determining Big
O Analyzing Algorithm Time Complexity Case Studies: Finding
Fibonacci Numbers Case Studies: Finding Greatest Common Divisors
Case Studies: Finding Prime Numbers Case Studies: Closest Pair of
Points Preview of Other AlgorithmsChapter 24 Sorting 24.1 24.2 24.3
24.4 24.5 24.6 24.7Introduction Bubble Sort Merge Sort Quick Sort
Heap Sort Bucket Sort and Radix Sort External SortChapter 25 Lists,
Stacks, Queues, and Priority Queues 25.1 25.2Introduction Common
Features for Lists707 708 708 710 712 713 714 716 719 727 728 728
729 730 737 738 742 745 746 748 751 756 765 766 766 767 769 771 774
778 783 785 791 792 792 794 797 801 807 809 821 822 822
23. Contents xxi 25.3 25.4 25.5 25.6 25.7 25.8Array Lists
Linked Lists Variations of Linked Lists Stacks and Queues Priority
Queues Case Study: Evaluating ExpressionsChapter 26 Binary Search
Trees 26.1 26.2 26.3 26.4 26.5 26.6Introduction Binary Search Trees
Deleting Elements in a BST Tree Visualization Iterators Case Study:
Data CompressionChapter 27 Graphs and Applications 27.1 27.2 27.3
27.4 27.5 27.6 27.7 27.8 27.9 27.10Introduction Basic Graph
Terminologies Representing Graphs Modeling Graphs Graph
Visualization Graph Traversals Depth-First Search (DFS)
Breadth-First Search (BFS) Case Study: The Nine Tail Problem Case
Study: The Knights Tour ProblemChapter 28 Weighted Graphs and
Applications 28.1 28.2 28.3 28.4 28.5 28.6Introduction Representing
Weighted Graphs The WeightedGraph Class Minimum Spanning Trees
Finding Shortest Paths Case Study: The Weighted Nine Tail
ProblemChapter 29 Multithreading 29.1 29.2 29.3 29.4 29.5 29.6 29.7
29.8Introduction Thread Concepts Creating Tasks and Threads The
Thread Class Example: Flashing Text GUI Event Dispatch Thread Case
Study: Clock with Audio Thread Pools825 830 842 843 846 847 857 858
858 870 876 879 881 891 892 893 894 898 909 911 912 916 919 923 939
940 940 942 949 955 962 971 972 972 972 975 978 979 980 983
24. xxii Contents 29.9 29.10 29.11 29.12 29.13 29.14 29.15
29.16 29.17 29.18 29.19Thread Synchronization Synchronization Using
Locks Cooperation among Threads Case Study: Producer/Consumer
Blocking Queues Semaphores Avoiding Deadlocks Thread States
Synchronized Collections SwingWorkerDisplaying Progress Using
JProgressBar985 989 991 995 998 1000 1001 1002 1002 1004
1007Chapter 30 Networking 30.1 30.2 30.3 30.4 30.5 30.6 30.7 30.8
30.91017 1018 1018 1025 1026 1029 1031 1036 1039 1041Introduction
Client/Server Computing The InetAddress Class Serving Multiple
Clients Applet Clients Sending and Receiving Objects Retrieving
Files from Web Servers JEditorPaneCase Studies: Distributed
TicTacToe GamesChapter 31 Internationalization 31.1 31.2 31.3 31.4
31.5 31.6Introduction The Locale Class Displaying Date and Time
Formatting Numbers Resource Bundles Character Encoding1057 1058
1058 1060 1071 1077 1084Chapter 32 JavaBeans and Bean Events 32.1
32.2 32.3 32.4 32.5 32.6Introduction JavaBeans Bean Properties Java
Event Model Review Creating Custom Source Components Creating
Custom Event Sets1091 1092 1092 1093 1094 1097 1101Chapter 33
Containers, Layout Managers, and Borders 33.1 33.2 33.3
33.4Introduction Swing Container Structures Layout Managers
Creating Custom Layout Managers1111 1112 1112 1114 1123
25. Contents xxiii 33.5 33.6 33.7 33.8JScrollPane JTabbedPane
JSplitPaneSwing BordersChapter 34 Menus, Toolbars, and Dialogs 34.1
34.2 34.3 34.4 34.5 34.6 34.7 34.8 34.9Introduction Menus Popup
Menus JToolBarProcessing Actions Using the Action Interface
JOptionPane Dialogs Creating Custom Dialogs JColorChooser
JFileChooserChapter 35 MVC and Swing Models 35.1 35.2 35.3 35.4
35.5 35.6 35.7 35.8 35.9 35.10Introduction MVC MVC Variations Swing
Model-View-Controller Architecture JSpinnerSpinner Models and
Editors JList and its ModelsList Models List Cell Renderer
JComboBox and its ModelsChapter 36 JTable and JTree 36.1 36.2 36.3
36.4 36.5 36.6 36.7 36.8 36.9 36.10 36.11 36.12 36.13 36.14
36.15Introduction JTableTable Models and Table Column Models Auto
Sort and Filtering Case Study: Modifying Rows and Columns Table
Renderers and Editors Custom Table Renderers and Editors Table
Model Events JTree TreeModel and DefaultTreeModel TreeNode,
MutableTreeNode, and DefaultMutableTreeNode TreePath and
TreeSelectionModel Case Study: Modifying Trees Tree Node Rendering
and Editing Tree Events1128 1132 1133 1136 1149 1150 1150 1156 1158
1160 1164 1171 1174 1176 1187 1188 1188 1194 1195 1196 1198 1205
1209 1212 1216 1225 1226 1226 1231 1235 1237 1242 1245 1247 1250
1254 1256 1259 1262 1265 1267
26. xxiv ContentsChapter 37 Java Database Programming 37.1 37.2
37.3 37.4 37.5 37.6 37.7Introduction Relational Database Systems
SQL JDBC PreparedStatement CallableStatementRetrieving Metadata1273
1274 1274 1278 1286 1295 1297 1300A detailed table of contents for
the Web chapters is available on the companion Web site:Chapter 38
Advanced Java Database Programming381Chapter 39 Servlets391Chapter
40 JavaServer Pages401Chapter 41 JSF and Visual Web
Development411Chapter 42 Web Services421Chapter 43 Remote Method
Invocation431Chapter 44 Java 2D44-1Chapter 45 AVL Trees and Splay
Trees45-1Chapter 46 2-4 Trees and B-Trees46-1Chapter 47 Red-Black
Trees47-1Chapter 48 Hashing48-1APPENDIXES Appendix A Appendix B
Appendix C Appendix D Appendix E Appendix FINDEXJava Keywords The
ASCII Character Set Operator Precedence Chart Java Modifiers
Special Floating-Point Values Number Systems1309 1312 1314 1316
1318 1319 1323
27. CHAPTER 1 INTRODUCTION TO COMPUTERS, PROGRAMS, AND JAVA
Objectives To review computer basics, programs, and operating
systems (1.21.4).To explore the relationship between Java and the
World Wide Web (1.5).To distinguish the terms API, IDE, and JDK
(1.6).To write a simple Java program (1.7).To display output on the
console (1.7).To explain the basic syntax of a Java program
(1.7).To create, compile, and run Java programs (1.8).(GUI) To
display output using the JOptionPane output dialog boxes
(1.9).
28. 2 Chapter 1 Introduction to Computers, Programs, and
Java1.1 Introduction why Java?You use word processors to write
documents, Web browsers to explore the Internet, and email programs
to send email. These are all examples of software that runs on
computers. Software is developed using programming languages. There
are many programming languagesso why Java? The answer is that Java
enables users to develop and deploy applications on the Internet
for servers, desktop computers, and small hand-held devices. The
future of computing is being profoundly influenced by the Internet,
and Java promises to remain a big part of that future. Java is the
Internet programming language. You are about to begin an exciting
journey, learning a powerful programming language. At the outset,
it is helpful to review computer basics, programs, and operating
systems and to become familiar with number systems. If you are
already familiar with such terms as CPU, memory, disks, operating
systems, and programming languages, you may skip the review in
1.21.4.1.2 What Is a Computer? hardware softwareA computer is an
electronic device that stores and processes data. It includes both
hardware and software. In general, hardware comprises the visible,
physical elements of the computer, and software provides the
invisible instructions that control the hardware and make it
perform specific tasks. Writing instructions for computers to
perform is called computer programming. Knowing computer hardware
isnt essential to your learning a programming language, but it does
help you understand better the effect of the program instructions.
This section introduces computer hardware components and their
functions. A computer consists of the following major hardware
components (Figure 1.1): Central processing unit (CPU)Memory (main
memory)Storage devices (e.g., disks, CDs, tapes)Input and output
devices (e.g., monitors, keyboards, mice, printers)Communication
devices (e.g., modems and network interface cards (NICs))
BusStorage DevicesMemoryCPUe.g., Disk, CD, and TapeCommunication
DevicesInput DevicesOutput Devicese.g., Modem and NICe.g.,
Keyboard, Mousee.g., Monitor, PrinterFIGURE 1.1 A computer consists
of CPU, memory, storage devices, input devices, output devices, and
communication devices. busThe components are connected through a
subsystem called a bus that transfers data or power between
them.1.2.1 Central Processing Unit CPUThe central processing unit
(CPU) is the computers brain. It retrieves instructions from memory
and executes them. The CPU usually has two components: a control
unit and an arithmetic/logic unit. The control unit controls and
coordinates the actions of the other
29. 1.2 What Is a Computer? 3 components. The arithmetic/logic
unit performs numeric operations (addition, subtraction,
multiplication, division) and logical operations (comparisons).
Todays CPU is built on a small silicon semiconductor chip having
millions of transistors. Every computer has an internal clock,
which emits electronic pulses at a constant rate. These pulses are
used to control and synchronize the pace of operations. The higher
the clock speed, the more instructions are executed in a given
period of time. The unit of measurement of clock speed is the hertz
(Hz), with 1 hertz equaling 1 pulse per second. The clock speed of
a computer is usually stated in megahertz (MHz) (1 MHz is 1 million
Hz). CPU speed has been improved continuously. Intels Pentium 3
Processor runs at about 500 megahertz and Pentium 4 Processor at
about 3 gigahertz (GHz) (1 GHz is 1000 MHz).speed hertz megahertz
gigahertz1.2.2 Memory To store and process information, computers
use off and on electrical states, referred to by convention as 0
and 1. These 0s and 1s are interpreted as digits in the binary
number system and called bits (binary digits). Data of various
kinds, such as numbers, characters, and strings, are encoded as
series of bits. Data and program instructions for the CPU to
execute are stored as groups of bits, or bytes, each byte composed
of eight bits, in a computers memory. A memory unit is an ordered
sequence of bytes, as shown in Figure 1.2. Memory address2000 2001
2002 2003 2004FIGURE 1.2bitbyteMemory content01001010 01100001
01110110 01100001 00000011Encoding for character J Encoding for
character a Encoding for character v Encoding for character a
Encoding for number 3Memory stores data and program
instructions.The programmer need not be concerned about the
encoding and decoding of data, which the system performs
automatically, based on the encoding scheme. In the popular ASCII
encoding scheme, for example, character 'J' is represented by
01001010 in one byte. A byte is the minimum storage unit. A small
number such as 3 can be stored in a single byte. To store a number
that cannot fit into a single byte, the computer uses several
adjacent bytes. No two data items can share or split the same byte.
A memory byte is never empty, but its initial content may be
meaningless to your program. The current content of a memory byte
is lost whenever new information is placed in it. A program and its
data must be brought to memory before they can be executed. Every
byte has a unique address. The address is used to locate the byte
for storing and retrieving data. Since bytes can be accessed in any
order, the memory is also referred to as random-access memory
(RAM). Todays personal computers usually have at least 1 gigabyte
of RAM. Computer storage size is measured in bytes, kilobytes (KB),
megabytes (MB), gigabytes (GB), and terabytes (TB). A kilobyte is
210 = 1024, about 1000 bytes, a megabyte is 220 = 1048576, about 1
million bytes, a gigabyte is about 1 billion bytes, and a terabyte
is about 1000 gigabytes. Like the CPU, memory is built on silicon
semiconductor chips having thousands of transistors embedded on
their surface. Compared to CPU chips, memory chips are less
complicated, slower, and less expensive.RAM megabyte
30. 4 Chapter 1 Introduction to Computers, Programs, and
Java1.2.3Storage DevicesMemory is volatile, because information is
lost when the power is turned off. Programs and data are
permanently stored on storage devices and are moved, when the
computer actually uses them, to memory, which is much faster than
storage devices. There are four main types of storage devices: CD
drives (CD-R, CD-RW, and DVD)Tape drivesdriveDisk drivesUSB flash
drivesDrives are devices for operating a medium, such as disks,
CDs, and tapes.Disks hard diskEach computer has at least one hard
drive. Hard disks are for permanently storing data and programs.
The hard disks of the latest PCs store from 80 to 250 gigabytes.
Often disk drives are encased inside the computer. Removable hard
disks are also available.CDs and DVDs CD-R CD-RWCD stands for
compact disk. There are two types of CD drives: CD-R and CD-RW. A
CD-R is for read-only permanent storage; the user cannot modify its
contents once they are recorded. A CD-RW can be used like a hard
disk and can be both read and rewritten. A single CD can hold up to
700 MB. Most software is distributed through CD-ROMs. Most new PCs
are equipped with a CD-RW drive that can work with both CD-R and
CD-RW. DVD stands for digital versatile disc or digital video disk.
DVDs and CDs look alike, and you can use either to store data. A
DVD can hold more information than a CD. A standard DVDs storage
capacity is 4.7 GB.Tapes Tapes are mainly used for backup of data
and programs. Unlike disks and CDs, tapes store information
sequentially. The computer must retrieve information in the order
it was stored. Tapes are very slow. It would take one to two hours
to back up a 1-gigabyte hard disk. The new trend is to back up data
using flash drives or external hard disks.USB Flash Drives USB
flash drives are devices for storing and transporting data. A flash
drive is smallabout the size of a pack of gum. It acts like a
portable hard drive that can be plugged into your computers USB
port. USB flash drives are currently available with up to 32 GB
storage capacity.1.2.4Input and Output DevicesInput and output
devices let the user communicate with the computer. The common
input devices are keyboards and mice. The common output devices are
monitors and printers.The Keyboardfunction key modifier keyA
computer keyboard resembles a typewriter keyboard with extra keys
added for certain special functions. Function keys are located at
the top of the keyboard and are numbered with prefix F. Their use
depends on the software. A modifier key is a special key (e.g.,
Shift, Alt, Ctrl) that modifies the normal action of another key
when the two are pressed in combination.
31. 1.3 Programs 5 The numeric keypad, located on the
right-hand corner of the keyboard, is a separate set of keys for
quick input of numbers. Arrow keys, located between the main keypad
and the numeric keypad, are used to move the cursor up, down, left,
and right. The Insert, Delete, Page Up, and Page Down keys, located
above the arrow keys, are used in word processing for performing
insert, delete, page up, and page down.numeric keypadThe Mouse A
mouse is a pointing device. It is used to move an electronic
pointer called a cursor around the screen or to click on an object
on the screen to trigger it to respond.The Monitor The monitor
displays information (text and graphics). The screen resolution and
dot pitch determine the quality of the display. The screen
resolution specifies the number of pixels per square inch. Pixels
(short for picture elements) are tiny dots that form an image on
the screen. A common resolution for a 17inch screen, for example,
is 1024 pixels wide and 768 pixels high. The resolution can be set
manually. The higher the resolution, the sharper and clearer the
image is. The dot pitch is the amount of space between pixels in
millimeters. The smaller the dot pitch, the better the
display.1.2.5screen resolutiondot pitchCommunication
DevicesComputers can be networked through communication devices,
such as the dialup modem (modulator/demodulator), DSL, cable modem,
network interface card, and wireless. A dialup modem uses a phone
line and can transfer data at a speed up to 56,000 bps (bits per
second). A DSL (digital subscriber line) also uses a phone line and
can transfer data twenty times faster. A cable modem uses the TV
cable line maintained by the cable company and is as fast as a DSL.
A network interface card (NIC) is a device that connects a computer
to a local area network (LAN). The LAN is commonly used in
universities and business and government organizations. A typical
NIC called 10BaseT can transfer data at 10 mbps (million bits per
second). Wireless is becoming popular. Every laptop sold today is
equipped with a wireless adapter that enables the computer to
connect with the Internet.modem DSL NIC LAN mbps1.3 Programs
Computer programs, known as software, are instructions to the
computer, telling it what to do. Computers do not understand human
languages, so you need to use computer languages in computer
programs. Programming is the creation of a program that is
executable by a computer and performs the required tasks. A
computers native language, which differs among different types of
computers, is its machine languagea set of built-in primitive
instructions. These instructions are in the form of binary code, so
in telling the machine what to do, you have to enter binary code.
Programming in machine language is a tedious process. Moreover, the
programs are highly difficult to read and modify. For example, to
add two numbers, you might have to write an instruction in binary
like this:software programmingmachine
language1101101010011010Assembly language is a low-level
programming language in which a mnemonic is used to represent each
of the machine-language instructions. For example, to add two
numbers, you might write an instruction in assembly code like this:
ADDF3 R1, R2, R3assembly language
32. 6 Chapter 1 Introduction to Computers, Programs, and Java
assemblerAssembly languages were developed to make programming
easy. However, since the computer cannot understand assembly
language, a program called an assembler is used to convert
assembly-language programs into machine code, as shown in Figure
1.3.Assembly Source File .. ADDF3 R1, R2, R3 ..FIGURE 1.3high-level
languageMachine-Code FileAssembler.. 1101101010011010 ..Assembler
translates assembly-language instructions to machine code.Assembly
programs are written in terms of machine instructions with
easy-to-remember mnemonic names. Since assembly language is machine
dependent, an assembly program can be executed only on a particular
kind of machine. The high-level languages were developed in order
to transcend platform specificity and make programming easier. The
high-level languages are English-like and easy to learn and
program. Here, for example, is a high-level language statement that
computes the area of a circle with radius 5: area = 5 * 5 *
3.1415;Among the more than one hundred high-level languages, the
following are well known: COBOL (COmmon Business Oriented
Language)FORTRAN (FORmula TRANslation)BASIC (Beginners All-purpose
Symbolic Instruction Code)Pascal (named for Blaise Pascal)Ada
(named for Ada Lovelace)C (developed by the designer of B)Visual
Basic (Basic-like visual language developed by Microsoft)Delphi
(Pascal-like visual language developed by Borland)C++ (an
object-oriented language, based on C)C# (a Java-like language
developed by Microsoft)JavaEach of these languages was designed for
a specific purpose. COBOL was designed for business applications
and is used primarily for business data processing. FORTRAN was
designed for mathematical computations and is used mainly for
numeric computations. BASIC was designed to be learned and used
easily. Ada was developed for the Department of Defense and is used
mainly in defense projects. C combines the power of an assembly
language with the ease of use and portability of a high-level
language. Visual Basic and Delphi are used in developing graphical
user interfaces and in rapid application development. C++ is
popular for system software projects such as writing compilers and
operating systems. The Microsoft Windows operating system was coded
using C++. C# (pronounced C sharp) is a new language developed by
Microsoft for developing applications based on the Microsoft .NET
platform. Java, developed by Sun Microsystems, is widely used for
developing platform-independent Internet applications.
33. 1.4 Operating Systems 7 A program written in a high-level
language is called a source program or source code. Since a
computer cannot understand a source program, a program called a
compiler is used to translate it into a machine-language program.
The machine-language program is then linked with other supporting
library code to form an executable file, which can be run on the
machine, as shown in Figure 1.4. On Windows, executable files have
extension .exe.Source FileCompilerMachine-language FileLinkersource
program compilerExecutable FileLibrary CodeFIGURE 1.4 A source
program is compiled into a machine-language file, which is then
linked with the system library to form an executable file.1.4
Operating Systems The operating system (OS) is the most important
program that runs on a computer, which manages and controls a
computers activities. The popular operating systems are Microsoft
Windows, Mac OS, and Linux. Application programs, such as a Web
browser or a word processor, cannot run without an operating
system. The interrelationship of hardware, operating system,
application software, and the user is shown in Figure
1.5.UserApplication ProgramsOperating SystemHardwareFIGURE 1.5The
operating system is the software that controls and manages the
system.The major tasks of an operating system are: Controlling and
monitoring system activitiesAllocating and assigning system
resourcesScheduling operations1.4.1 Controlling and Monitoring
System Activities Operating systems perform basic tasks, such as
recognizing input from the keyboard, sending output to the monitor,
keeping track of files and directories on the disk, and controlling
peripheral devices, such as disk drives and printers. They also
make sure that different programs and users running at the same
time do not interfere with each other, and they are responsible for
security, ensuring that unauthorized users do not access the
system.OS
34. 8 Chapter 1 Introduction to Computers, Programs, and
Java1.4.2Allocating and Assigning System ResourcesThe operating
system is responsible for determining what computer resources a
program needs (e.g., CPU, memory, disks, input and output devices)
and for allocating and assigning them to run the
program.1.4.3multiprogrammingmultithreadingmultiprocessingScheduling
OperationsThe OS is responsible for scheduling programs to make
efficient use of system resources. Many of todays operating systems
support such techniques as multiprogramming, multithreading, or
multiprocessing to increase system performance. Multiprogramming
allows multiple programs to run simultaneously by sharing the CPU.
The CPU is much faster than the computers other components. As a
result, it is idle most of the timefor example, while waiting for
data to be transferred from the disk or from other sources. A
multiprogramming OS takes advantage of this situation by allowing
multiple programs to use the CPU when it would otherwise be idle.
For example, you may use a word processor to edit a file at the
same time as the Web browser is downloading a file. Multithreading
allows concurrency within a program, so that its subtasks can run
at the same time. For example, a word-processing program allows
users to simultaneously edit text and save it to a file. In this
example, editing and saving are two tasks within the same
application. These two tasks may run on separate threads
concurrently. Multiprocessing, or parallel processing, uses two or
more processors together to perform a task. It is like a surgical
operation where several doctors work together on one patient.1.5
Java, World Wide Web, and Beyond This book Gosling at embedded
redesignedappletintroduces Java programming. Java was developed by
a team led by James Sun Microsystems. Originally called Oak, it was
designed in 1991 for use in chips in consumer electronic
appliances. In 1995, renamed Java, it was for developing Internet
applications. For the history of Java, see
java.sun.com/features/1998/05/birthday.html. Java has become
enormously popular. Its rapid rise and wide acceptance can be
traced to its design characteristics, particularly its promise that
you can write a program once and run it anywhere. As stated by Sun,
Java is simple, object oriented, distributed, interpreted, robust,
secure, architecture neutral, portable, high performance,
multithreaded, and dynamic. For the anatomy of Java
characteristics, see
www.cs.armstrong.edu/liang/JavaCharacteristics.pdf. Java is a
full-featured, general-purpose programming language that can be
used to develop robust mission-critical applications. Today, it is
employed not only for Web programming, but also for developing
standalone applications across platforms on servers, desktops, and
mobile devices. It was used to develop the code to communicate with
and control the robotic rover on Mars. Many companies that once
considered Java to be more hype than substance are now using it to
create distributed applications accessed by customers and partners
across the Internet. For every new project being developed today,
companies are asking how they can use Java to make their work
easier. The World Wide Web is an electronic information repository
that can be accessed on the Internet from anywhere in the world.
The Internet, the Webs infrastructure, has been around for more
than thirty years. The colorful World Wide Web and sophisticated
Web browsers are the major reason for the Internets popularity. The
primary authoring language for the Web is the Hypertext Markup
Language (HTML). HTML is a simple language for laying out
documents, linking documents on the Internet, and bringing images,
sound, and video alive on the Web. However, it cannot interact with
the user except through simple forms. Web pages in HTML are
essentially static and flat. Java initially became attractive
because Java programs can be run from a Web browser. Such programs
are called applets. Applets employ a modern graphical interface
with buttons,
35. 1.5 Java, World Wide Web, and Beyond 9 text fields, text
areas, radio buttons, and so on, to interact with users on the Web
and process their requests. Applets make the Web responsive,
interactive, and fun to use. Figure 1.6 shows an applet running
from a Web browser for playing a Tic Tac Toe game. Enter this URL
from a Web browserFIGURE 1.6A Java applet for playing TicTacToe is
embedded in an HTML page.Tip For a demonstration of Java applets,
visit java.sun.com/applets. This site provides a rich Java resource
as well as links to other cool applet demo sites. java.sun.com is
the official Sun Java Website.Java can also be used to develop
applications on the server side. These applications can be run from
a Web server to generate dynamic Web pages. The automatic grading
system for this book, as shown in Figure 1.7, was developed using
Java. Enter this URL from a Web browserFIGURE 1.7Java was used to
develop an automatic grading system to accompany this book.
36. 10 Chapter 1 Introduction to Computers, Programs, and Java
Java is a versatile programming language. You can use it to develop
applications on your desktop and on the server. You can also use it
to develop applications for small handheld devices. Figure 1.8
shows a Java-programmed calendar displayed on a BlackBerry and on a
cell phone.FIGURE 1.8 Java can be used to develop applications for
hand-held and wireless devices, such as a BlackBerry (left) and a
cell phone (right).1.6 The Java Language Specification, API, JDK,
and IDE Java language specificationAPIJava SE, EE, and MEJDK 1.6 =
JDK 6Java IDEComputer languages have strict rules of usage. If you
do not follow the rules when writing a program, the computer will
be unable to understand it. The Java language specification and
Java API define the Java standard. The Java language specification
is a technical definition of the language that includes the syntax
and semantics of the Java programming language. The complete Java
language specification can be found at java.sun.com/docs/books/jls.
The application program interface (API) contains predefined classes
and interfaces for developing Java programs. The Java language
specification is stable, but the API is still expanding. At the Sun
Java Website (java.sun.com), you can view and download the latest
version of the Java API. Java is a full-fledged and powerful
language that can be used in many ways. It comes in three editions:
Java Standard Edition (Java SE), Java Enterprise Edition (Java EE),
and Java Micro Edition (Java ME). Java SE can be used to develop
client-side standalone applications or applets. Java EE can be used
to develop server-side applications, such as Java servlets and
JavaServer Pages. Java ME can be used to develop applications for
mobile devices, such as cell phones. This book uses Java SE to
introduce Java programming. There are many versions of Java SE. The
latest, Java SE 6, will be used in this book. Sun releases each
version with a Java Development Toolkit (JDK). For Java SE 6, the
Java Development Toolkit is called JDK 1.6 (also known as Java 6 or
JDK 6). JDK consists of a set of separate programs, each invoked
from a command line, for developing and testing Java programs.
Besides JDK, you can use a Java development tool (e.g., NetBeans,
Eclipse, and TextPad)software that provides an integrated
development environment (IDE) for rapidly developing Java programs.
Editing, compiling, building, debugging, and
37. 1.7 A Simple Java Program 11 online help are integrated in
one graphical user interface. Just enter source code in one window
or open an existing file in a window, then click a button, menu
item, or function key to compile and run the program.1.7 A Simple
Java Program Let us begin with a simple Java program that displays
the message Welcome to Java! on the console. Console refers to text
entry and display device of a computer. The program is shown in
Listing 1.1.consoleLISTING 1.1 Welcome.javaVideo Note First Java
program1 public class Welcome { 2 public static void main(String[]
args) { 3 // Display message Welcome to Java! to the console 4
System.out.println("Welcome to Java!"); 5 } 6 }class main method
display messageWelcome to Java!The line numbers are displayed for
reference purposes but are not part of the program. So, dont type
line numbers in your program. Line 1 defines a class. Every Java
program must have at least one class. Each class has a name. By
convention, class names start with an uppercase letter. In this
example, the class name is Welcome. Line 2 defines the main method.
In order to run a class, the class must contain a method named
main. The program is executed from the main method. A method is a
construct that contains statements. The main method in this program
contains the System.out.println statement. This statement prints a
message "Welcome to Java!" to the console (line 4). Every statement
in Java ends with a semicolon (;), known as the statement
terminator. Reserved words, or keywords, have a specific meaning to
the compiler and cannot be used for other purposes in the program.
For example, when the compiler sees the word class, it understands
that the word after class is the name for the class. Other reserved
words in this program are public, static, and void. Line 3 is a
comment that documents what the program is and how it is
constructed. Comments help programmers to communicate and
understand the program. They are not programming statements and
thus are ignored by the compiler. In Java, comments are preceded by
two slashes (//) on a line, called a line comment, or enclosed
between /* and */ on one or several lines, called a block comment.
When the compiler sees //, it ignores all text after // on the same
line. When it sees /*, it scans for the next */ and ignores any
text between /* and */. Here are examples of comments:line
numbersclass name main methodstatement terminator reserved
wordcomment// This application program prints Welcome to Java! /*
This application program prints Welcome to Java! */ /* This
application program prints Welcome to Java! */A pair of braces in a
program forms a block that groups the programs components. In Java,
each block begins with an opening brace ({) and ends with a closing
brace (}). Every class has a class block that groups the data and
methods of the class. Every method has a methodblock
38. 12 Chapter 1 Introduction to Computers, Programs, and Java
block that groups the statements in the method. Blocks can be
nested, meaning that one block can be placed within another, as
shown in the following code. public class Welcome { public static
void main(String[] args) { Class block System.out.println("Welcome
to Java!"); Method block } }Tip matching bracesAn opening brace
must be matched by a closing brace. Whenever you type an opening
brace, immediately type a closing brace to prevent the
missing-brace error. Most Java IDEs automatically insert the
closing brace for each opening brace.Note You are probably
wondering why the main method is declared this way and why
System.out.println(...) is used to display a message to the
console. For the time being, simply accept that this is how things
are done. Your questions will be fully answered in subsequent
chapters.Caution case sensitiveJava source programs are case
sensitive. It would be wrong, for example, to replace main in the
program with Main.Note syntax rulesLike any programming language,
Java has its own syntax, and you need to write code that obeys the
syntax rules. If your program violates the rulesfor example if the
semicolon is missing, a brace is missing, a quotation mark is
missing, or String is misspelledthe Java compiler will report
syntax errors. Try to compile the program with these errors and see
what the compiler reports.The program in Listing 1.1 displays one
message. Once you understand the program, it is easy to extend it
to display more messages. For example, you can rewrite the program
to display three messages, as shown in Listing 1.2.LISTING 1.2
Welcome1.java class main method display message1 public class
Welcome1 { 2 public static void main(String[] args) { 3
System.out.println("Programming is fun!"); 4
System.out.println("Fundamentals First"); 5
System.out.println("Problem Driven"); 6 } 7 }Programming is fun!
Fundamentals First Problem DrivenFurther, you can perform
mathematical computations and display the result to the console.
10.5 + 2 * 3 Listing 1.3 gives an example of evaluating . 45 -
3.5
39. 1.8 Creating, Compiling, and Executing a Java Program
13LISTING 1.3 ComputeExpression.java 1 public class
ComputeExpression { 2 public static void main(String[] args) { 3
System.out.println((10.5 + 2 * 3) / (45 3.5)); 4 } 5 }class main
method compute expression0.39759036144578314The multiplication
operator in Java is *. As you see, it is a straightforward process
to translate an arithmetic expression to a Java expression. We will
discuss Java expressions further in Chapter 2.1.8 Creating,
Compiling, and Executing a Java Program You have to create your
program and compile it before it can be executed. This process is
repetitive, as shown in Figure 1.9. If your program has compilation
errors, you have to modify the program to fix them, then recompile
it. If your program has runtime errors or does not produce the
correct result, you have to modify the program, recompile it, and
execute it again.Create/Modify Source Code Source code (developed
by the programmer) Saved on the disk public class Welcome { public
static void main(String[] args) { System.out.println("Welcome to
Java!"); Source Code } }Bytecode (generated by the compiler for JVM
to read and interpret, not for you to understand) Method Welcome()
0 aload_0 Method void main(java.lang.String[]) 0 getstatic #2 3 ldc
#3 5 invokevirtual #4 8 returnCompile Source Code e.g., javac
Welcome.java If compilation errors Stored on the disk BytecodeRun
Bytecode e.g., java WelcomeWelcome to Java is printed on the
console Welcome to Java!Result If runtime errors or incorrect
resultFIGURE 1.9 The Java program-development process consists of
repeatedly creating/modifying source code, compiling, and executing
programs.
40. 14 Chapter 1 Introduction to Computers, Programs, and Java
You can use any text editor or IDE to create and edit a Java
source-code file. This section demonstrates how to create, compile,
and run Java programs from a command window. If you wish to use an
IDE such as Eclipse, NetBeans, or TextPad, please refer to
Supplement II for tutorials. From the command window, you can use
the NotePad to create the Java source code file, as shown in Figure
1.10.editorVideo Note Brief Eclipse TutorialFIGURE 1.10You can
create the Java source file using Windows NotePad.Note The source
file must end with the extension .java and must have exactly the
same name as the public class name. For example, the file for the
source code in Listing 1.1 should be named Welcome.java, since the
public class name is Welcome.file nameA Java compiler translates a
Java source file into a Java bytecode file. The following command
compiles Welcome.java: javac Welcome.javacompileNote You must first
install and configure JDK before compiling and running programs.
See Supplement I.B, Installing and Configuring JDK 6, on how to
install JDK and set up the environment to compile and run Java
programs. If you have trouble compiling and running programs,
please see Supplement I.C, Compiling and Running Java from the
Command Window. This supplement also explains how to use basic DOS
commands and how to use Windows NotePad and WordPad to create and
edit files. All the supplements are accessible from the Companion
Website.Supplement I.BSupplement I.CIf there are no syntax errors,
the compiler generates a bytecode file with a .class extension. So
the preceding command generates a file named Welcome. class, as
shown in Figure 1.11(a). The Java language is a high-level language
while Java bytecode is a low-level language. The bytecode is
similar to machine instructions but is architecture neutral and can
run on any platform that has a Java Virtual Machine (JVM), as shown
in Figure 1.11(b). Rather than a physical machine, the virtual
machine is a program that interprets Java bytecode. This is one of
Javas primary advantages: Java bytecode can run on a variety of
hardware platforms and operating systems..class bytecode
filecompiled byJava Compiler(a)generatesWelcome.class (Java
bytecode executable file)JVM executed byirtual Mach aVe
inWelcome.java (Java source code file)JavBytecode JavaAny
Computer(b)FIGURE 1.11 (a) Java source code is translated into
bytecode. (b) Java bytecode can be executed on any computer with a
Java Virtual Machine.
41. 1.8 Creating, Compiling, and Executing a Java Program 15 To
execute a Java program is to run the programs bytecode. You can
execute the bytecode on any platform with a JVM. Java bytecode is
interpreted. Interpreting translates the individual steps in the
bytecode into the target machine-language code one at a time rather
than translating the whole program as a single unit. Each step is
executed immediately after it is translated. The following command
runs the bytecode: java Welcomeinterpreting bytecoderunFigure 1.12
shows the javac command for compiling Welcome.java. The compiler
generated the Welcome.class file. This file is executed using the
java command.CompileVideo Note Compile and run a Java programShow
filesRunFIGURE 1.12The output of Listing 1.1 displays the message
Welcome to Java!Note For simplicity and consistency, all source
code and class files are placed under c:book unless specified
otherwise.c:bookCaution Do not use the extension .class in the
command line when executing the program. Use java ClassName to run
the program. If you use java ClassName.class in the command line,
the system will attempt to fetch ClassName.class.class.java
ClassNameTip If you execute a class file that does not exist, a
NoClassDefFoundError will occur. If you execute a class file that
does not have a main method or you mistype the main method (e.g.,
by typing Main instead of main), a NoSuchMethodError will
occur.NoClassDefFoundError NoSuchMethodErrorNote When executing a
Java program, the JVM first loads the bytecode of the class to
memory using a program called the class loader. If your program
uses other classes, the class loader dynamically loads them just
before they are needed. After a class is loaded, the JVM uses a
program called bytecode verifier to check the validity of the
bytecode and ensure that the bytecode does not violate Javas
security restrictions. Java enforces strict security to make sure
that Java programs arriving from the network do not harm your
computer.class loader bytecode verifier
42. 16 Chapter 1 Introduction to Computers, Programs, and Java
Pedagogical Note Instructors may require students to use packages
for organizing programs. For example, you may place all programs in
this chapter in a package named chapter1. For instructions on how
to use packages, please see Supplement I.F, Using Packages to
Organize the Classes in the Text.using package1.9 (GUI) Displaying
Text in a Message Dialog Box JOptionPane showMessageDialogThe
program in Listing 1.1 displays the text on the console, as shown
in Figure 1.12. You can rewrite the program to display the text in
a message dialog box. To do so, you need to use the
showMessageDialog method in the JOptionPane class. JOptionPane is
one of the many predefined classes in the Java system that you can
reuse rather than reinventing the wheel. You can use the
showMessageDialog method to display any text in a message dialog
box, as shown in Figure 1.13. The new program is given in Listing
1.4. Title Title bar Message Click the OK button to dismiss the
dialog boxFIGURE 1.13Welcome to Java! is displayed in a message
box.LISTING 1.4 WelcomeInMessageDialogBox.java block
commentimportmain method display messagepackage1 2 3 4 5 6 7 8 9 10
11/* This application program displays Welcome to Java! * in a
message dialog box. */ import javax.swing.JOptionPane; public class
WelcomeInMessageDialogBox { public static void main(String[] args)
{ // Display Welcome to Java! in a message dialog box
JOptionPane.showMessageDialog(null, "Welcome to Java!"); } }This
program uses a Java class JOptionPane (line 9). Javas predefined
classes are grouped into packages. JOptionPane is in the
javax.swing package. JOptionPane is imported to the program using
the import statement in line 4 so that the compiler can locate the
class without the full name javax.swing.JOptionPane.Note If you
replace JOptionPane on line 9 with javax.swing.JOptionPane, you
dont need to import it in line 4. javax.swing.JOptionPane is the
full name for the JOptionPane class.The showMessageDialog method is
a static method. Such a method should be invoked by using the class
name followed by a dot operator (.) and the method name with
arguments. Methods will be introduced in Chapter 5, Methods. The
showMessageDialog method can be invoked with two arguments, as
shown below. JOptionPane.showMessageDialog(null, "Welcome to
Java!");
43. Key Terms 17 The first argument can always be null. null is
a Java keyword that will be fully introduced in Chapter 8, Objects
and Classes. The second argument is a string for text to be
displayed. There are several ways to use the showMessageDialog
method. For the time being, you need to know only two ways. One is
to use a statement, as shown in the example:two versions of
showMessageDialogJOptionPane.showMessageDialog(null, x);where x is
a string for the text to be displayed. The other is to use a
statement like this one: JOptionPane.showMessageDialog(null, x, y,
JOptionPane.INFORMATION_MESSAGE);where x is a string for the text
to be displayed, and y is a string for the title of the message
box. The fourth argument can be JOptionPane.INFORMATION_MESSAGE,
which causes the icon ( ) to be displayed in the message box, as
shown in the following example.JOptionPane.showMessageDialog(null,
"Welcome to Java!", "Display Message",
JOptionPane.INFORMATION_MESSAGE);Note There are two types of import
statements: specific import and wildcard import. The specific
import specifies a single class in the import statement. For
example, the following statement imports JOptionPane from package
javax.swing.specific importimport javax.swing.JOptionPane;The
wildcard import imports all the classes in a package. For example,
the following statement imports all classes from package
javax.swing.wildcard importimport javax.swing.*;The information for
the classes in an imported package is not read in at compile time
or runtime unless the class is used in the program. The import
statement simply tells the compiler where to locate the classes.
There is no performance difference between a specific import and a
wildcard import declaration.no performance differenceNote Recall
that you have used the System class in the statement
System.out.println(Welcome to Java); in Listing 1.1. The System
class is not imported because it is in the java.lang package. All
the classes in the java.lang package are implicitly imported in
every Java program.KEY TERMS .class file 14 .java file 14 assembly
language 5 bit 3 block 12 block comment 11 bus 2byte 3 bytecode 14
bytecode verifier 15 cable modem 5 central processing unit (CPU)
class loader 15 comment 112java.langimplicitly imported
44. 18 Chapter 1 Introduction to Computers, Programs, and Java
compiler 7 console 11 dot pitch 5 DSL (digital subscriber line) 5
hardware 2 high-level language 6 Integrated Development Environment
(IDE) 10 java command 15 javac command 15 Java Development Toolkit
(JDK) 10 Java Virtual Machine (JVM) 14 keyword (or reserved word)
11 line comment 11 machine language 5 main method 11memory 3 modem
5 network interface card (NIC) operating system (OS) 7 pixel 5
program 5 programming 5 resolution 5 software 5 source code 7
source file 14 specific import 17 storage devices 4 statement 11
wildcard import 175Note Supplement I.AThe above terms are defined
in the present chapter. Supplement I.A, Glossary, lists all the key
terms and descriptions in the book, organized by chapters.CHAPTER
SUMMARY 1. A computer is an electronic device that stores and
processes data. 2. A computer includes both hardware and software.
3. Hardware is the physical aspect of the computer that can be
seen. 4. Computer programs, known as software, are the invisible
instructions that control the hardware and make it perform tasks.5.
Computer programming is the writing of instructions (i.e., code)
for computers to perform.6. The central processing unit (CPU) is a
computers brain. It retrieves instructions from memory and executes
them.7. Computers use zeros and ones because digital devices have
two stable states, referred to by convention as zero and one.8. A
bit is a binary digit 0 or 1. 9. A byte is a sequence of 8 bits.
10. A kilobyte is about 1000 bytes, a megabyte about 1 million
bytes, a gigabyte about 1 billion bytes, and a terabyte about 1000
gigabytes.11. Memory stores data and program instructions for the
CPU to execute. 12. A memor