Introduction to Programming Using Java
Version 5.0, December 2006
Version 5.1.2, with minor corrections and updates, June 2010
Introduction to Programming Using Java is a free introductory computer programming textbook that uses Java as the language of instruction. It is suitable for use in an introductory programming course and for people who are trying to learn programming on their own. There are no prerequisites beyond a general familiarity with the ideas of computers and programs. There is enough material for a full year of college-level programming. Chapters 1 through 7 can be used as a textbook in a one-semester college-level course or in a year-long high school course.
This version of the book covers "Java 5.0". It also works well with later versions of Java. (While Java 5.0 introduced major new features that need to be covered in an introductory programming course, Java 6.0 and the upcoming Java 7.0 do not.) Many of the examples in the book use features that were not present before Java 5.0. Note that Java applets appear throughout the pages of this book. Many of those applets will be non-functional in Web browsers that do not support Java 5.0.
The home web site for this book is http://math.hws.edu/javanotes/. The page at that address contains links for downloading a copy of the web site and for downloading a PDF version of the book.
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.)
There are several approaches to teaching Java. One approach uses 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. After an introductory chapter, I cover procedural programming in Chapters 2, 3, and 4. Object-oriented programming is introduced in Chapter 5. Chapters 6 covers the closely related topic of event-oriented programming and graphical user interfaces. Arrays are covered in Chapter 7. Chapter 8 marks a turning point in the book, moving beyond the fundamental ideas of programming to cover more advanced topics. Chapter 8 is mostly about writing robust and correct programs, but it also has a section on parallel processing and threads. Chapters 9 and 10 cover recursion and data structures, including the Java Collection Framework. Chapter 11 is about files and networking. Finally, Chapter 12 returns to the topic of graphical user interface programming to cover some of Java's more advanced capabilities.
Major changes were made for the fifth edition of this book. Perhaps the most significant change is the use of parameterized types in the chapter on generic programming. Parameterized types -- Java's version of templates -- were the most eagerly anticipated new feature in Java 5.0.
Other new features in Java 5.0 are also covered. Enumerated types are introduced, although they are not covered in their full complexity. The "for-each" loop is covered and is used extensively. Formatted output is also used extensively, and the Scanner class is covered (though not until Chapter 11). Static import is covered briefly, as are variable arity methods.
The non-standard TextIO class that I use for input in the first half of the book has been rewritten to support formatted output. I have also added some file I/O capabilities to this class to make it possible to cover some examples that use files early in the book.
Javadoc comments are covered for the first time in the fifth edition. Almost all code examples have been revised to use Javadoc-style comments.
The coverage of graphical user interface programming has been reorganized, much of it has been rewritten, and new material has been added. In previous editions, I emphasized applets. Stand-alone GUI applications were covered at the end, almost as an afterthought. In the fifth edition, the emphasis on applets is gone, and almost all examples are presented as stand-alone applications. However, applet versions of each example are still presented on the web pages of the on-line version of the book. The chapter on advanced GUI programming has been moved to the end, and a significant amount of new material has been added, including coverage of some of the features of Graphics2D.
Aside from the changes in content, the appearance of the book has been improved, especially the appearance of the PDF version. For the first time, the quality of the PDF approaches that of conventional textbooks.
Version 5.1 of this book is a minor update of Version 5.0. A number of typographical and coding errors in Version 5.0 have been corrected. Also, the discussion of the Eclipse IDE in Section 2.6 has been updated to be consistent with more recent versions of Eclipse.
The latest complete edition of Introduction to Programming using Java is always available on line at http://math.hws.edu/javanotes/. The first version of the book was written in 1996, and there have been several editions since then. All editions are archived at the following Web addresses:
- First edition: http://math.hws.edu/eck/cs124/javanotes1/ (Covers Java 1.0.)
- Second edition: http://math.hws.edu/eck/cs124/javanotes2/ (Covers Java 1.1.)
- Third edition: http://math.hws.edu/eck/cs124/javanotes3/ (Covers Java 1.1.)
- Fourth edition: http://math.hws.edu/eck/cs124/javanotes4/ (Covers Java 1.4.)
- Fifth edition: http://math.hws.edu/eck/cs124/javanotes5/ (Covers Java 5.0.)
Introduction to Programming using Java is free, but it is not in the public domain. As of Version 5.0, it is published under the terms of the Creative Commons Attribution-Share Alike 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA. This license allows redistribution and modification under certain terms. For example, you can:
- Post an unmodified copy of the on-line version on your own Web site (including the parts that list the author and state the license under which it is distributed!).
- Give away or sell printed, unmodified copies of this book, as long as they meet the requirements of the license.
- Make modified copies of the complete book or parts of it and post them on the web or otherwise distribute them, provided that attribution to the author is given, the modifications are clearly noted, and the modified copies are distributed under the same license as the original. This includes translations to other languages.
While it is not actually required by the license, I do appreciate hearing from people who are using or distributing my work.
A technical note on production: The on-line and PDF versions of this book are created from a single source, which is written largely in XML. To produce the PDF version, the XML is processed into a form that can be used by the TeX typesetting program. In addition to XML files, the source includes DTDs, XSLT transformations, Java source code files, image files, a TeX macro file, and a couple of scripts that are used in processing.
I have made the complete source files available for download at the following address:
These files were not originally meant for publication, and therefore are not very cleanly written. Furthermore, it requires a fair amount of expertise to use them effectively. However, I have had several requests for the sources and have made them available on an "as-is" basis. For more information about the source and how they are used see the README file from the source download.