This book is designed to teach you the Java programming language, and even more importantly, to teach you basic programming techniques. This book requires no previous programming experience and no mathematics other than some very simple high school algebra. However, to get the full benefit of the book, you should have a version of Java available on your computer, so that you can practice with the examples and techniques given in the book. You should have a version of Java called Java 2 (or some number higher than 2). If you have a version number of the form 1.1.x or 1.2.x, then the version number should be 1.2.x or higher. (The exact number that is filled in for the x is not critical. The x need not even be present. If it says only "version 1.2," that is fine.)
You need not have any previous programming experience to use this book. This book was designed for beginners. However, the book can still be used to learn Java if you happen to have had experience with some other programming languages, but allow me to give you a few words of advice. If you have programmed before, do not assume that Java is the same as the programming language(s) you are used to using. All languages are different. And the differences, even if small, are large enough to give you problems. Read at least the boxed sections of Section 1.4 in Chapter 1 and all the boxed sections of Chapters 2 and 3. By the time you reach Chapter 4, it would be wise to read the entire chapter.
If you have programmed before in either C or C++, the transition to Java can be troublesome. While Java is very different from C and C++, at first glance it looks as if it is the same as C++. Appendix 11 has a comparison of Java and C++ that will help you see the differences between Java and C++ (or Java and C).
This book contains a CD that includes all the programs and other software examples in the book, so that you can practice with these examples without having to type them into your computer.
How and what version of Java you use depends somewhat on what operating system you are using. Be sure to consult the subsection below that corresponds to your operating system.
The CD that comes with this book includes a version of JBuilder 3.5 Foundation, a complete Java integrated environment from Inprise/Borland. JBuilder includes an editor and other utilities in addition to the Java language. This has everything you need to write and run Java programs. This is a professional strength environment, which can be a bit complex for novices, so we also have an alternative that gives you an easier environment.
This is a bit more complicated to initially set up, but easier to use once you do set up things. Download a free Java compile over the Internet from Sun Microsystems. Install that Java compiler and the TextPad environment, which is provided on the CD that comes with this book. The TextPad environment provides an editor and other tools to use when writing Java programs.
At the time this book went to press, the site for the Java compiler download from Sun Microsystems was:
There is a version of Java for the Mac that can be downloaded from the Sun Microsystems website. Unfortunately, users have not been happy with the Mac version of Java provided at this site, and indeed, it may not do all things discussed in this book.
If you are using the Mac operating system, one good alternative is to purchase a version of CodeWarrior from Metrowerks, Inc. It works well with the Mac operating system.
A version of JBuilder for the Mac is due out soon and promises be an excellent alternative for Mac users. You may want to check the following website to see if it is available. If it is, you can download it from there.
The CD that comes with this book includes a version of JBuilder 3.5 Foundation, a complete Java integrated environment from Inprise/Borland. JBuilder includes an editor and other utilities in addition to the Java language. This has all the software you need in order to write and run Java programs. JBuilder has versions for both the Solaris and Linux operating systems.
You can down load a free Java compiler over the Internet from Sun Microsystems. At the time this book went to press, the site for the Java download from Sun Microsystems was:
We do not have an editor/environment (other than JBuilder) that we recommend for use with this compiler. You can use your favorite editor to write programs and then run your Java programs from the command line as described in Chapter 1. (Or you may find an environment you like and can use it.)
Each chapter contains numerous self-test questions. Complete answers for all the self-test questions are given at the end of each chapter. One of the best ways to practice what you are learning is to do the self-test questions without looking at the answers. Only look at the answers after you have answered the self-test questions.
In addition to using this book as a textbook, you can and should use it as a reference. When you need to check a particular point that you may have forgotten or that you hear mentioned by somebody but have not yet learned yourself, just look in the index. Many index entries give a page number for "quick reference." Turn to this quick reference page. It will contain a short entry, usually set off in a box, that gives all the essential points on that topic. This can be done to check details of the Java language, as well as details on programming techniques.
Boxed sections in every chapter give you a quick summary of the main points in that chapter. You can use these boxes to review the chapter, preview the chapter, or check details of the Java language.
Any updates or corrections will be listed on the author's website for this book
This book was written for you, and I would like to hear any comments you have on the book. You can contact me via electronic mail at the following address:
Unfortunately, I cannot provide you with answers to the programming exercises. Only instructors who adopt the book can receive (selected) answers from the publisher. For help on the programming exercises, you will have to contact your instructor. (Even if you are not enrolled in a class we still cannot provide answers to programming exercises.) But, remember that there are answers to all the self-test questions at the end of each chapter.
This book was designed to be used in a first course in programming and computer science. It covers programming techniques, as well as the basics of the Java programming language. It is suitable for courses as short as one quarter or as long as one full academic year. It requires no previous programming experience and no mathematics other than a little high school algebra. This book can also be used for a course designed to teach Java to students who have already had another programming course, in which case, the first few chapters can be assigned as outside reading. (If students have had previous programming experience in C or C++, then there is also an appendix that explains some differences between Java and C or C++.) All the code in the book has been tested using Java 2 of Sun Microsystems. The coverage of Java was carefully arrived at by class testing and is a concise, accessible introduction for beginners.
If you have not used the first edition of this text, you can skip this subsection. If you have used the first edition, this subsection will tell you how this second edition differs from the first edition.
For instructors, the transition from the first edition of this text to this edition is easy. You can teach the same course with basically the same topics presented in the same order. Some chapters have changed numbers, but you can still cover those chapters in the order you are currently using. The biggest change was to move the arrays chapter forward to Chapter 6. However, you can cover arrays later if you prefer with no loss of continuity in reading the text. The only significant change you will need to contend with is that this edition uses the Swing library instead of using only the AWT library as the first edition did. However, there have been changes and additions that you may find helpful.
This edition adds coverage of the Swing Libraries, the Graphics class, and linked data structures to the topics covered in the first edition. In addition, the entire book has been rewritten to make the material clearer and more complete. There are many more Self-Test Questions and many more Programming Exercises in this edition.
In response to requests from users of the first edition, we have adopted the policy of listing instance variables first in class definitions (as opposed to last, as in the first edition).
This book also contains some early, optional material on applets and another GUI class named JOptionPane . This allows instructors to introduce GUI interfaces early if they wish, or wait to introduce them later (or not at all) if that is preferred.
The first edition of this book was already fully compatible with Java 2. This edition adds coverage of Swing and other Java 2 details to provide more complete coverage of Java 2.
If you are an instructor, this book adapts to the way you teach, rather than making you adapt to the book. This book does not tightly prescribe the order in which your course must cover topics. Neither does it prescribe the specialized libraries that must be used in your course. You can easily change the order in which chapters and sections are covered. The details about rearranging material are explained in a chart at the end of this preface and in more details in a prerequisite section at the start of each chapter.
Since Java does not include any simple console input, most texts, even more advanced texts, provide some added class library for console input. This book requires that you add as little nonstandard software as possible, since only one simple class is added (for console input). Even that one console input class, which is included early in the book, becomes an understandable programming example for students well before the end of the book. All the remaining software is from standard Java libraries that should be part of any Java installation.
This book is designed to teach students basic problem-solving and programming techniques and is not simply a Java syntax book. The book contains numerous case studies and programming tips, as well as many other sections that explain important problem-solving and programming techniques, such as loop design techniques, debugging techniques, style techniques, abstract data types, basic object-oriented programming including event-driven programming, and other computer science topics.
Any course that really teaches Java must teach classes early, since almost everything in Java involves classes. The behavior of parameters depends on whether they are class parameters. Even the behavior of the equals operator (==) depends on whether it is comparing objects or simpler data items. Classes cannot be avoided, except by means of absurdly long and complicated "magic formulas." This book introduces classes fairly early. Some exposure to using classes is introduced in Chapters 1 and 2. Defining classes is covered in Chapter 4. Moreover, all the basic information about classes, including inheritance, is presented by the end of Chapter 7 (and this can be done omitting Chapter 6). However, some topics on classes, including inheritance, can be postponed to later in a course.
Although this is an early classes book, it does not neglect traditional programming techniques, such as top-down design and loop design techniques. These older topics may no longer be glamorous, but they are information that all beginning students need.
Starting with Java 2, Java comes with an improved GUI library known as Swing that allows programmers to design portability GUIs (graphical user interfaces). This book uses Swing to teach students to produce professional looking windowing interfaces. In the process, students learn event-driven programming, as well as receiving a lot of practice with object-oriented programming.
As this material was class-tested and views of instructors were gathered, we found that Swing was a more accessible way to teach students object-oriented programming than applets. Thus, we place greater emphasis on Swing. This makes sense, since almost all advanced applets tools are really Swing tools. However, for those who do want to cover applets early, Chapter 1 has an optional section that previews applets. Chapter 13 covers applets in detail and may be covered much earlier than the chapter number suggests. You may choose to introduce GUIs early, late, or not at all.
With the introduction of the Swing libraries, there is a new class named JOptionPane that allows an easier introduction to GUIs than applets provide. This book covers JOptionPane in an optional section of Chapter 2. You have the choice of introducing either or both applets and JOptionPane either late or early (or not at all).
In addition to this optional GUI material in Chapters 1 and 2, this book includes three full chapters on GUIs, which gives thorough coverage of Swing, applets, and the Graphics class for simple two-dimensional graphics.
This book teaches programming technique and does not simply teach the Java language. However, neither students nor instructors would be satisfied with an introductory programming course that did not also teach the programming language. Until you calm a student's fears about language details, it is often impossible to get her or his attention to discuss bigger issues. For this reason, this book gives complete explanations of Java language features and lots of sample code. Programs are given in their entirety along with sample input and output. In many cases, there are even extra complete examples on the CD, in addition to the complete examples in the text.
Self-test questions are spread throughout each chapter. These questions have a wide range of difficulty levels. Some require only a one-word answer, whereas others require the reader to write an entire, nontrivial program. Complete answers for all the self-test questions, including those requiring full programs, are given at the end of each chapter.
The material in this book has been fully class tested. Much of the material and methods of presentation were revised in response to this class testing.
The support materials described below can be obtained from the publisher, obtained over the Internet, or are included with the book.
Each book contains a CD that includes all the programs and classes in the book. The CD also includes a version of JBuilder 3.5 Foundation, a complete Java integrated environment from Inprise/Borland. JBuilder includes an editor and other utilities in addition to the Java language. The CD includes versions of JBuilder for Windows, Solaris, and Linux operating systems. The CD also includes a copy of TextPad, a very nice integrated environment that runs under Windows and that is a suitable environment for use with Sun's Java 2.
You have a wide choice of free software to use with this book. As already noted the CD that comes with this book includes JBuilder and TextPad.
JBuilder works under Windows, Solaris, and Linux operating systems. At the time this book went to press a version of JBuilder for the Mac was not available, but was due out soon. You may want to check the following website to see if it is available. If it is, you can download it from there.
Another very good alternative is to download a version of Java-2 from Sun Microsystems website. At the time this book went to press, the URL for the website was:
Java-2 is the main version of Java that we used in developing this text. The TextPad environment (which comes on the CD that accompanies this book) is a good environment to use with Sun's Java-2, provided you are using a Windows operating system. TextPad only runs under Windows.
Instructor tools include a chapter-by-chapter Instructor's Resource Guide that contains numerous teaching hints, quiz questions with solutions, and solutions to many programming exercises. The Companion Website includes code, PowerPoint slides, and other teaching resources. Instructors should contact their Prentice Hall sales representative to obtain a copy of the Instructor's Resource Guide and receive information on how to access the Companion Website. For the name and number of your sales representative, please call Prentice Hall Faculty Services at 1-800-526-0485. Additional information on this book and other Prentice Hall products can be found on Prentice Hall's website at
Any updates or corrections will be listed on the author's website for this book
I thank the Computer Science and Engineering Department of the University of California, San Diego (UCSD), which is my home department and the place that I tested much of this material. Many students in my classes were kind enough to help correct preliminary versions of this text. These student comments and the comments of instructors who class tested this book were a tremendous help in shaping the final book. In particular, I extend a special thanks to Carole McNamee of California State University, Sacramento and to both Paul Kube and Susan Marx of UCSD; their feedback and class testing of earlier editions or drafts of the book was a great help to me in producing this edition.
I thank all the reviewers who took the time to read drafts of this or the previous edition of this book. They provided invaluable detailed comments and suggestions. In alphabetical order within each group, they are
Tom Cortina--SUNY, Stony Brook
Prasun Dewan--University of North Carolina
Laird Dornan--Sun Microsystems
H.E. Dunsmore--Purdue, Lafayette
Adel Elmaghraby--University of Louisville
Ric Heishman--North Virginia C.C.
Dr. Le Gruenwald--University of Oklahoma
Michael Clancy--University of California, Berkeley
Michael Godfrey--Cornell University
Robert Herrmann--Sun Microsystems, Java Soft
Robert Holloway--University of Wisconsin, Madison
Lily Hou--Carnegie-Mellon University
John Motil--California State University, Northridge
James Roberts--Carnegie-Mellon University
Nan C. Schaller--Rochester Institute of Technology
I also thank all the individuals at Prentice Hall who organized the reviewing and production of this book. In particular, I thank Jake Warde for a masterful job of coordinating the entire processes including the reviews, Jerry Ralya my developmental editor for his excellent work in all aspects of the writing on this edition, Toni Holm for her work in coordinating things between offices, and to Gail Cocker, Heather Scott, and especially Scott Disanno for work on the design and production of the book. All these wonderful people cheerfully did a great job. I extend a special thanks to my publisher Alan Apt for his invaluable support and advice throughout the writing and production process.
I thank Lew Rakocy for his excellent work on the programming exercises added to this edition. I thank Brian Durney for his fine work on the instructor's support material.
I thank Sun Microsystems for allowing me to use the Duke icon in a number of my GUI examples.
Finally, I give an extra special thanks to Christina for putting up with me while I worked late on this book and for even going so far as to proofread some sections of the book for me.