Java Gently is a first programming text. It aims to teach students, as well as those fascinated by the possibilities of their computers and the Internet, how to program, and how to do it in the best possible style in the Java programming language. In the process, Java Gently covers all the fudamental structures of the Java 2 language and most of its core libraries and utilities. In terms of programming, it covers object-orientation, software design, structured programming, graphical user interfacing, event-driven programming, multi-threading, networking and an introduction to data structures.

A philosophy of teaching Java

Teaching Java as a first language is now the accepted thing to do in universities, colleges and even high schools. Educators have realised that Java has several unique features that make it suitable not only as a language that will equip students for future courses and careers, but also make it a language that students want to learn. Java is fun, Java is interesting, and Java is not all that hard if taught in a careful way. What are these features?

1. Java is small, encompassing a limited number of features that can be used sensibly together.

2. Java is object-oriented, which enables programs to reflect the real-world and reduces repetition and errors.

3. Java provides for full graphical user interface (GUI) and multimedia (sound, image and animation) facilities, and promotes GUI programs as applets associated with web browsers.

4. Java provides built-in multithreading features and facilitates programming on the network in a variety of ways.

Given this list there seem to be two possible approaches to presenting Java as a first teaching language. The first is immediately attractive: start at point 3; go straight into GUI, multimedia, applets and the whole network environment, thereby catching the students’ interest and showing them how modern and different Java really is. The second approach is to start at the beginning, laying a sound foundation of basic concepts, and then moving on to the newer aspects.

Java Gently does some of both, as they both have advantages. Starting with applets and GUI is exciting, but requires so much underlying knowledge of objects that even if students learn the outer shell of programs parrot-fashion, they may never fully understand what they are doing. On the other hand, proceeding at a steady pace through all the background first is a sound policy, but can be both boring and overwhelming.

Java Gently’s approach

The approach I have devised is three pronged and hinges on Java’s three main selling points: it is network-enabled, small and fosters re-use. So we start off with programs that show how the web is used, but then move straight into covering the basic concepts. In so doing, we introduce the idea of packages (libraries or APIs) early on and employ three especially written for the book. These get students started on meaningful input-output in an object-oriented way, without hiding basic concepts. Then applets, multimedia, graphics and so on can be introduced properly, to students who have already encountered classes, objects, instantiation and inheritance, and know how to handle them.

Approach is not everything, however, and Java Gently uses three other tried and tested teaching techniques:

• diagrams everywhere,

• explanation by example,

• multiple-choice quizzes, comprehensions and problems after every chapter.

There are also three kinds of diagrams used throughout the book:

  1. model diagrams, which show the structure of the program at the class and object level, while still revealing the basic components such as methods and variables and the interaction between them;
  2. form diagrams, which show the syntax and semantics of each new Java construct in a way that mirrors its usage in practice.
  3. algorithm diagrams, which show the flow of small programs, and also serve to illustrate new concepts such as exceptions and multi-threading;

In the third edition, definite improvements have been made to the diagrams in the book. Firstly, the class diagram notation has been made easier to draw and has been brought into line with UML (although UML itself is not appropriate for programming at this level). Then the form diagrams have been expanded to include a semantics section and all the forms are collected at the end of the book to form a useful reference to the language constructs and selected APIs. The algorithms have also been reworked for clarity.


There are over 80 complete fully worked examples, together with test data and output and 50% of these are new in the third edition. Each example has been carefully chosen so that it both illustrates the feature that has just been introduced, and also solves a real problem in the best possible Java style. Most of the examples have a ‘real world’ flavour, which reinforces the need for a programmer to understand the user’s problem, and to write programs in a user-friendly way. For example, new examples whih occur several time in the book are:

As soon as a new feature is introduced, it becomes part of the repertoire of the programmer, and will re-emerge in subsequent examples when needed. As the book progresses, the examples get longer and more challenging.

Teaching programming vs teaching Java

For a first-year course, it is no longer sufficient to just teach a language. The expectation of today’s computer users is that the whole computing milieu will assist in the solution of day-to-day problems. In addition to explaining syntax and the construction of a well-formed program, a programming textbook for the next century has to include techniques for problem solving.

Java Gently is indeed such a modern textbook. Each worked problem – and there are over 80 of them – is introduced with a typically inexact statement. This is then refined in the process of devising a solution. Where applicable, the appropriate technique is selected from those previously discussed, and then we proceed to algorithm development. As an additional aid, algorithms are illustrated with structured diagrams, and important techniques are discussed, highlighted and identified for reuse later on. The important programming paradigms of:

• structured programming,

• object-oriented programming,

• class design,

• abstraction,

• software reuse, and

• generality

all receive attention, and examples are carefully chosen to show how these techniques can encourage correctness and efficiency. For example, there are discussions on guidelines for class design, on the relationship between arrays and classes, on the interplay between loops and exceptions, and on the different ways of implementing data structures, to mention just a few.

Order of topics

The topics are grouped into chapters to enable an easy introduction to programming right at the beginning. The order of statements as covered is: output, assignment, for, input, if, while, do, switch. This grouping does not follow that of the Java reference manual, nor of a book intended for someone who already knows programming. It is a pedagogically tried and tested order, which gets the student through the fundamentals of Java, gently, but without undue delay and fuss.

Most of the chapters contain an even mix of program, control and data structure issues, plus some Java specialities. My experience has shown that this integrated approach to the order of topics has tremendous benefits for motivation and understanding, and that students are able to get ahead and accomplish more in a shorter time, without compromising their assimilation of the principles of Java and programming. The key points of the order of all the material are:

• rapid entry to genuine programming problems;

• the power of object-oriented programming presented with realistic examples that use more than one class and more than two objects right from the start;

• exceptions introduced early on and explained as a natural way to control flow between classes;

• ‘no fuss’ discussion of features that are simple in Java (such as class structure) but in-depth treatment of potentially difficult topics (such as casting and cloning);

• hash tables grouped with arrays to show their efficacy in providing for non-integer indices;

• three custom-made input-output packages, which are used from Chapter 4, and explained in full in Chapters 7 and 14;

• almost full coverage of the awt (Abstract Windowing Toolkit) facilities, but geared towards examples, together with an intoduction to Java’s Swing set;

• applets presented through a viewer and a browser, and the differences between applets and applications explained once the student can genuinely appreciate the issues;

• multi-threading used for animation and the development of interesting applications.

• socket programming to illustrate simple client–server or peer-to-peer systems such as chat servers and RMI to show the power of Java in a distributed environment;

• database access via an ODBC-JDBC bridge to a common database system, with simple to understand set up steps;

• coverage of some of the JFC (Java Foundation Collection) for algorithms and data structures ranging from sorting and searching through stacks, queus, lists and bit sets.

The details of the books coveraged are presented as a table in the Appendix.

Teaching aids and the web site

Java Gently contains:

• over 80 fully worked examples and case studies;

• summaries after each chapter;

• multiple choice quizzes at the end of each chapter, with solutions;

• over 100 exercises;

• an active web site;

• special Display, Stream and Graph classes for augmenting Java’s input-output;

• notations for algorithms and models resulting in over 70 diagrams;

• a unique way of presenting Java syntax and semantics to the novice programmer.

All examples have been tested and run and are available on a web site which will be actively maintained and updated. The web site at

also contains:

• web pages which introduce the book,

• all the examples for downloading, either individually, in chapters or in one go;

• Frequently Asked Questions;

• error list;

• discussion board;

• future plans for the book;

• e-mail contact with the author and the Java Gently team.

Who the book is for

The book is intended for students learning to program for the first time in Java, and who have access to the Java Development Kit from Sun, running on PCs, Macs or Unix. There is no reliance in the book on any of the myriad of development environments that are available. Students would normally be in their first year at a university or college, and could be in the science, engineering, commerce or liberal arts faculties: the examples are sufficiently wide-ranging to cater for all. The book does not require mathematical experience, and would certainly be accessible for school pupils taking computer science in senior years. Because there is an emphasis on facts and examples, rather than long discussions, the book would also be suitable for experienced programmers or hobbyists who wish to pick up Java quickly.

The book is based on many courses given in Java since 1996, and on courses in other languages presented to science and engineering students at first year university level since 1980. Included are many of the class-tested examples and exercises from these courses. All examples have been tested on JDK 1.3 on Intel PCs with Windows NT and Linux, and the majority of them also on an iMac and PowerBook G3 with Java 1.1.6 on Mac OS9 and CodeWarrior 4. The browser used for the applets was primarily NetScape Communicator with HotJava and Internet Explorer occasionally.

Because technology, and especially Java, is advancing at a rapid rate, it may be that a new version of Java is available simultaneously with this book. Consult our web site for the latest information and for program updates.


This third edition of Java Gently has been greatly influenced and improved by the many people from all round the world who took the trouble to write in and share their experiences and ideas. In particular, I would like to acknowledge the input of Jens KaasBøll of the University of Oslo and Uwe Kastens of the University of Paderborn who made extensive critiques of the object oriented parts of the book.

Once again I am grateful to the technical staff and research students in the department who continued to help solve numerous problems of a rapidly moving Java on multiple platforms, in particular Basil Worrall who has devoted many hours to ensuring that the programs are correct and compatible with the text and the website, and in spotting errors. Basil is also responsible for the model diagrams, for the database example, and for updating several of the later programs. I could not have wished for a more able and faithful assistant, but any errors that remain are my own.

Johnny Lo has also kept the Java Gently web site up to date and running, answering questions and queries on a daily basis with Basil. The contributions of earlier members of the Java Gently team must be acknowledged once more: Louis Botha, Tony Abbott, Alwyn Moolman, John Botha and Daniel Acton. I thank my department for the equipment provided, and for the non-stop dial-in service which enabled me to work late into the night. Doing so would also not have been possible without Beethoven, Brahms and the Beatles, and the cheerful emails from all my friends on the Pacific West Coast.

The software we used came from Sun, Microsoft and Apple and I pay tribute to these giants in the computer industry for the ease of use and stability that we so often take for granted. The book was prepared on an iMac with Microsoft Work 98 and most of the programs developed there using CodeWarrior 4, and on a Windows NT workstation using JDK 1.3 and TextPad 4, all a pleasure to work with.

Pearson Education have once again provided outstanding professional service in the editorial and production departments in getting this book to print on time: my gratitude especially to Martin Sugden who put up with more than he had bargained for, and to Kate Brewin, Michael Klopstock, Bridget Allen, Ros Woodward and their teams.

Finally, as always, I thank Nigel for his support and assistance with the difficult problems that always occur late at night, and to my dear sons William and Michael for their understanding and patience with my months of preoccupation with the book. They are still learning Pascal, but I hope that when they enrol at university soon, they will reap the benefits of their forbearance, and enjoy Java as I do.

Judith M Bishop,
Pretoria, South Africa
September 2000