Introduction to Programming with Java

Preface


"INTRODUCTION TO PROGRAMMING WITH JAVA" is the "second edition" of an on-line introductory programming textbook that uses Java as the language of instruction. This book does not claim to cover the Java language comprehensively, although it does cover enough of it to make it possible to write interesting programs and applets. The main point of the text, however, is to teach the basics of programming -- including object-oriented programming -- with no prerequisites except a general familiarity with the ideas of computers and programs.

This text should be useful to anyone who wants to learn Java, but who is not already an expert in C and C++. Unlike many introductions to Java programming, it does not assume any background in these languages.

Many working applets are included on the Web pages that make up the text, and the full source code for all these applets can be found in an appendix.

I used the "first edition" of the text in introductory programming courses taught at Hobart and William Smith Colleges in Fall 1996 and Winter 1998. The second edition has been updated to cover Java 1.1 instead of Java 1.0 and was used in the Fall term of 1998. The course has a weekly lab. Lab worksheets from Fall 1998 and from previous terms are available. (See the information page for CS124.)


Usage Restrictions

This on-line text can be freely used for non-commercial purposes, as long as its source and author are made clear. For example, you can download a copy and use it on your own computer. You can post it in unmodified form on your own Web server (provided that you do not charge for access). You can print it out for your personal use. Professors who use it in a course can make printed copies and make them available to students for the cost of reproduction.

The text can also be distributed in unmodified form as part of a CD-ROM collection of free and/or shareware materials, provided that the cost of the CD is not more than $50.

Anyone who wants to use the text for any other purposes that might be considered "commercial" should contact me for permission.


Downloading the Text

This entire text is available for downloading in several formats. You can use the following links to download the archives. I assume that you know how to unpack the archives for use on your computer.

The "first edition" of the text, which covered Java 1.0 instead of Java 1.1, is also available for download. See the bottom of its index page at http://math.hws.edu/eck/cs124/notes98.html.


Why a Free On-line Text?

You might ask, does this really qualify as a textbook? And if so, why is it available for free on-line, instead of as an overpriced hardcover edition?

To answer the first question: Yes, this is meant as a serious textbook. Currently, it is not quite as long as most programming textbooks, but it has plenty of material for a solid one-term course. I think that it is a reasonable choice for a textbook in a college-level programming course -- or I wouldn't be using it in my own courses.

When I started work on the text for the Fall term of 1996, there was really no suitable textbook for introductory programming in Java. I decided to write my own class notes, and it seemed reasonable to put them in HTML format so that I could include working Java applets right on the page. I felt that the result was good enough to publish on the Web, and the response to it has been good. I suppose that I had some idea that I might eventually convert the notes into a hard-copy textbook, but I know from experience that it's a long, hard process to get a textbook into print -- and not a very profitable one unless a lot of people buy the book.

Since then, I've decided that the book really works well in an on-line version. Sometimes, it would be convenient to have a printed version as well, but if I ever do come out with a printed version, it will be a companion to the on-line version, rather than vice versa.

Furthermore, in the meantime, I've become a fan of the Linux operating system and the whole free software movement. (The "free" in this case means "freely distributable" rather than "free of charge.") If we can have free software, why not free textbooks?


Java 1.0 vs. Java 1.1

Java 1.1 introduced a large number of changes to the Java language, and in this second edition of the text, I have made correspondingly large changes. I do not try to cover both Java 1.0 and 1.1. That is, I almost never say things like, in Java 1.1 you do this, but in Java 1.0 you do that. And I don't try to point out the features that were unavailable in Java 1.0. It's time to let Java 1.0 fade away...

However, it is only fairly recently that Web browsers have become available that use Java 1.1. If you read this text with an older browser, most of the applets will just show up as blank white areas. Netscape 4.0.6, released in August 1998, is the first version of Netscape that will run the Java 1.1 applets in these notes. (On the Macintosh, even Netscape 4.0.6 does not support Java 1.1.) Internet Explorer 4.0 also uses Java 1.1, as does Sun Microsystem's browser, HotJava 1.1.4.


Changes from the First Edition

Chapter 1 is almost unchanged, except that I've removed Section 8, which was an explanation of why I decided to use Java instead of C++ in my introductory programming class. I don't think this can any longer be seen as a controversial decision.

In the fist edition, Chapters 2 and 3 used a "Console" class that I wrote for doing console-style I/O in programs. I did this because I found standard input and output (System.in and System.out) to be undependable. (The Macintoshes on which I first taught the course did not even implement standard input!) In the second edition, I use a "TextIO" class that simply provides a reasonable interface to the standard input and output streams. This makes for a smoother exposition, since using the Console class forced me to start using objects prematurely.

I've added a new section in Chapter 2 on "the structure of Java programs," in which I try to deal with the confusion that results from having both static and non-static members in classes.

I restructured the material in Chapter 4 extensively, without really adding any important new topics.

The largest changes in the text are in Chapters 5 and 6, which have been completely rewritten to use the Java 1.1 event model. All the applets in the text (except for some of the decorative end-of-chapter applets) have been rewritten to use this event model. I've added sections in Chapter 6 on nested classes and on Frames, and I moved the section on threads and animation from Chapter 6 to Chapter 5. The number of sample applets in Chapters 5 and 6 has been increased substantially.

Chapter 7 contains a new section that briefly introduces some of Java's standard data types, such as StringBuffer and HashTable. The rest of the chapter is little changed

Chapter 8 has been revised to cover Reader and Writer streams. These were introduced in Java 1.1 as the recommended way to do character input and output, in place of InputStream and OutputStream. InputStream and OutputStream are still used for binary data.

Chapter 9 is essentially unchanged (and might be removed in future editions of this text).


The Future of This Text

I expect that there will be a "third edition" of this text, but not until the second half of the year 2000. I will be on sabbatical for the academic year 1999--2000, so I won't be teaching any courses. However, I do plan to work on this text as one of my sabbatical projects.

It looks like Java is here to stay as an important language. The next version of the language, Java 1.2, will be out before the end of 1998. As far as I know, nothing in Java 1.2 will require major changes in this text. One of the big changes in Java 1.2 will be the inclusion of a new set of GUI components, called "Swing," as an alternative to the AWT components used in Java 1.0 and 1.1. If Swing becomes popular enough to displace the AWT, then I will probably rewrite the text to use Swing instead of the AWT. Most of the other forseeable changes in Java concern advanced API's that will probably never be more than mentioned in an introductory text

I would like to expand treatment of several topics in the text. In the next edition, Chapter 7 will be broken into at least two chapters. The first chapter will cover arrays, probably with more examples than are now included. The second chapter will include material on linked data structures such as trees, stacks, and queues. It will also include an introduction to recursion. Chapter 8, which in this edition is pretty sketchy, will also be expanded and possibly broken into separate chapters on writing correct and robust programs, using files and streams, and networking. In the longer term, the text might eventually be expanded to include enough material for a two-term introductory programming sequence.