Introduction to Programming Using Java,
Fourth Edition (Version 4.0)
"INTRODUCTION TO PROGRAMMING USING JAVA" is a free, on-line textbook. It is suitable for use in an introductory programming course and for people who are trying to learn programming on their own. There is probably enough material for a full year College programming course. There are no prerequisites beyond a general familiarity with the ideas of computers and programs.
In style, this is a textbook rather than a tutorial. That is, it concentrates on explaining concepts rather than giving step-by-step how-to-do-it guides. I have tried to use a conversational writing style that might be closer to classroom lecture than to a typical textbook. You'll find programming exercises at the end of most chapters, and you will find a detailed solution for each exercise, with the sort of discussion that I would give if I presented the solution in class. (I strongly advise that you read the exercise solutions if you want to get the most out of this book.) This is certainly not a Java reference book, and it is not even close to a comprehensive survey of all the features of Java. It is not written as a quick introduction to Java for people who already know another programming language. Instead, it is directed mainly towards people who are learning programming for the first time, and it is as much about general programming concepts as it is about Java in particular. I believe that Introduction to Programming using Java is fully competitive with the conventionally published, printed programming textbooks that are available on the market. (Well, all right, I'll confess that I think it's better.)
This version of the book covers "Java 2", the version of Java that was introduced with version 1.2 of the Java Software Development Kit. It was written using version 1.3 of the development kit and should work with later versions as well. The current version, which as of July 2002 is J2SE SDK 1.4.0, can be downloaded from Sun Microsystem's Java page at http://java.sun.com/. ("J2SE SDK 1.4.0" stands for Java 2 Standard Edition Software Development Kit Version 1.4.0, and it is often referred to by its older acronym, JDK, which stands for Java Development Kit. Sun has not exactly been sensible about the way it names things.)
The previous versions of this book used Java 1.1. For a long time, I was reluctant to move to Java 2 because it has been much less widely supported and because most of the new features don't represent new programming concepts. I have finally decided to make the change for several reasons. First of all, there is one genuinely new concept in Java 2: generic programming. I have added a chapter on this topic. Second, Java 2 is, after some initial roughness, working well and can be used with a variety of Web browsers. Third, there is the upcoming switch from C++ to Java in the High School Advanced Placement Test in Computer Programming. I have taken the opportunity of a new version to make the book more compatible with the requirements of that test. There is more information below on the changes that I have made in the new version.
There are applets embedded in the pages of this book. If you want to see the applets running in your Web browser, you will need one that supports Java 2. Unfortunately, Microsoft's version of Java, which is used by default in Internet Explorer on Windows, does not support it. However, if you install the Java Software Development Kit, the installer will give you a chance to use Sun's version of Java with Internet Explorer. If you do that, Internet Explorer will be able to run all Java applets, not just old ones. (This might not work in all versions of Windows.) As far as I know, all Web browsers for MacOS X use Apple's version of Java, which supports Java 2, and should work fine. MacOS 9 and earlier, however, will never run Java 2. On Linux, the new Mozilla 1.0 Web browser runs Java 2 reasonably well, but in my experience, it still crashes regularly when it is used with Java. (I have a lot of experience since I use Mozilla on Linux as my primary browser.) Every version of Java seems to get bigger. If you are unable to run Java 2, remember that Version 3 of this book only requires Java 1.1, and it covers most of the same basic programming concepts.
There are several approaches to teaching Java. One approach uses applets and graphical user interface programming from the very beginning. Some people believe that object oriented programming should also be emphasized from the very beginning. This is not the approach that I take. The approach that I favor starts with the more basic building blocks of programming and builds from there. I cover procedural programming in Chapters 2, 3, and 4. Object-oriented programming is introduced in Chapter 5. Chapters 6 and 7 cover the closely related topic of event-oriented programming and graphical user interfaces. Arrays are covered in Chapter 8, with more coverage of data structures in Chapters 11 and 12. Chapter 10 covers files and input/output streams. Chapter 9 covers exception handling, which is a prerequisite for using files in Java, and uses the opportunity for a more general discussion of the problem of writing correct and robust programs.
The current edition of Introduction to Programming using Java will always be available at the following Web address:
All editions are still available and are permanently archived at the following Web addresses:
First edition: http://math.hws.edu/eck/cs124/javanotes1/
Second edition: http://math.hws.edu/eck/cs124/javanotes2/
Third edition: http://math.hws.edu/eck/cs124/javanotes3/
Fourth edition: http://math.hws.edu/eck/cs124/javanotes4/
Changes from the Previous Edition
The big change in this edition, of course, is the switch from Java 1 to Java 2. Java 2 did not change the fundamentals of the Java language, but it did introduce many new features. The two that affect this book are the "Swing" graphical user interface library and the framework for generic programming. Java 1 used something called the AWT for GUI programming. Swing has more features and is more sophisticated. I have rewritten all the GUI programming examples to use Swing, and I have extensively rewritten the parts of the book that cover GUI programming. Because Swing is so complex, there are many parts of it that I do not cover, but I do cover enough to write real GUI programs. The other important new feature is a set of "Collection" and "Map" classes that represent generic, reusable data structures. Generic programming has become an important topic, and I have added a new chapter to cover this material.
Java is scheduled to be used as the programming language for the High School Advanced Placement test in computer science starting in the academic year 2003-2004. I've heard from several high school teachers who have used the previous version of this book in their classes, and of several more who are considering using it. Although the previous version already covered most of the AP material, I have made a few changes to improve the coverage. For example, I moved preconditions and postconditions for subroutines from Chapter 9 to Chapter 4, I changed examples that used the Vector class to use the ArrayList class instead, and I briefly introduce the class java.util.Random. There are two versions of the AP test. A course that covered Chapters 1 though 5 and Chapter 8 would include essentially all the material required for the "A" test. Sections 5.5 and 5.6 could be omitted. For the "AB" test, all of Chapter 5 and topics from Chapters 9 through 12 should be added. The AP exam does not require any GUI programming, so Chapters 6 and 7 could be omitted entirely. But a Java course with no graphical programming would be missing out on a lot of the fun.
Here is more detailed chapter-by-chapter description of the changes:
- Chapters 1, 2, and 3 are almost unchanged, except for mentions of Swing in Sections 1.6 and 3.7.
- Chapter 4: I've added some material to Section 4.6 on preconditions and postconditions for subroutines and included a mention of the javax package in Section 4.5.
- Chapter 5: I added some material on java.util.Random and ArrayList and rewrote an example, ShapeDraw, to use ArrayList instead of Vector. I moved material on nested classes from Chapter 7 to Chapter 5. This material plus Section 5.5 was then split into two sections, 5.5 and 5.6. Some of the material from the old 5.5 has moved into Section 5.4.
- Chapter 6 and 7 were throughly rewritten to use Swing, although the set of concepts that is covered is actually not much changed. Besides the more significant changes, I dropped most of the coverage of threads in favor of timers, since timers are used instead of threads for animation in Swing. I also dropped material on using double-buffering for animation, since double buffering is automatic in Swing. I dropped Section 6.7, which was a short description of event-handling in Java 1.0.
- Chapters 8 through 11: Mostly unchanged, although all graphical examples and exercises have been rewritten to use Swing. Also, I added ArrayLists to Section 8.3, and I rewrote the material on networking in Sections 10.4 and 10.5. Some of the material on threads that I dropped from Chapter 7 is now in Section 10.5.
- Chapter 12, on generic programming, is all new.
- Appendices: I removed the old Appendix 1, which was a description of C++ for Java programmers, and replaced it with a short description of some of the advanced features of Java that are not covered in the text. I rewrote Appendix 2, which is about Java programming environments. And of course, many of the source code files in Appendix 3 have been rewritten to use Java 2.
Introduction to Programming using Java is free, but it is not in the public domain. As of Version 4.0, it is published under the terms of the GNU Free Documentation License. This license allows redistribution and modification under certain terms. For example, you can:
- Post an unmodified copy of this textbook on your own Web site.
- Give away or sell printed, unmodified copies of this book, as long as they meet the requirements of the license.
- Post on the web or otherwise distribute modified copies, provided that the modifications are clearly noted in accordance with the license.
While it is not actually required by the license, I do appreciate hearing from people who are using or distributing my work.
Professor David J. Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, New York 14456, USA