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 ("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:
Second edition:
Third edition:
Fourth edition:

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:

Usage Restrictions

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:

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

July, 2002

[ Main Index ]