Introduction to Programming Using Java
Version 8.0, December 2018
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. The remaining chapters can be covered in a second course.
The Eighth Edition of the book covers Java 8, and it uses JavaFX 8 for GUI programming. Although later versions of Java have been released, I currently have no plans to cover them in this textbook. (However, all sample programs and exercise solutions have been compiled using Java 11 as well as Java 8.)
The home web site for this book is http://math.hws.edu/javanotes8/. The page at that address contains links for downloading a copy of the web site and for downloading PDF versions of the book. The web site—and the web site download—includes source code for the sample programs that are discussed in the text, answers to end-of-chapter quizzes and a discussion and solution for each end-of-chapter exercise. Readers are encouraged to download the source code for the examples and to read and run the programs as they read the book. Readers are also strongly encouraged to read the exercise solutions if they want to get the most out of this 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. This is certainly not a Java reference book, and it is not 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. Chapter 6 covers the closely related topic of event-oriented programming and graphical user interfaces. Arrays are introduced in Chapter 3 with a full treatment in Chapter 7. Chapter 8 is a short chapter that marks a turning point in the book, moving beyond the fundamental ideas of programming to cover more advanced topics. Chapter 8 is about writing robust, correct, and efficient programs. Chapters 9 and 10 cover recursion and data structures, including generic programming and the Java Collection Framework. Chapter 11 is about files and networking. Chapter 12 covers threads and parallel processing. Finally, Chapter 13 returns to the topic of graphical user interface programming to cover some of JavaFX's more advanced capabilities.
The biggest change from the seventh edition of this textbook is a switch from Swing to JavaFX for GUI programming. With just a few exceptions in code that the user is not meant to read, all example code and exercises use JavaFX. (Unfortunately, even as I was working on the new edition, the future of JavaFX has become unclear because, as of Java 11, JavaFX is no longer included as part of the basic Java download. However, it is still be available to be downloaded and installed separately.) The eighth edition also incorporates some features that were introduced in Java 8: lambda expressions and the stream API. Lambda expressions are covered fairly early, as part of the material on subroutines in Chapter 4. They are used extensively with JavaFX in Chapter 6 and Chapter 13, as well as with the stream API, which is introduced in Section 10.6. Aside from these major changes, there are small improvements throughout, such as a short discussion of loop invariants and class invariants in Section 8.2, increased coverage of Java's higher level concurrency support in Chapter 12, and the consistent use of factory methods like Integer.valueOf(n) in preference to constructors.
The first version of the book was written in 1996, and there have been several editions since then. All editions are archived (at least until my retirement) 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.)
- Sixth edition: http://math.hws.edu/eck/cs124/javanotes6/ (Covers Java 5.0, with a bit of 6.0.)
- Seventh edition: http://math.hws.edu/eck/cs124/javanotes7/ (Covers Java 7.)
- Eighth edition: http://math.hws.edu/eck/cs124/javanotes8/ (Covers Java 8.)
Introduction to Programming using Java is free, but it is not in the public domain. Version 8 is published under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/. 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 unmodified copies of this book or sell them at cost of production, 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 non-commercially, 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.
For uses of the book in ways not covered by the license, permission of the author is required.
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. The scripts work on Linux and on Mac OS.
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. However, I have had several requests for the sources and have made them available on an "as-is" basis. For more information about the sources and how they are used see the README file from the source download.
Professor David J. Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
300 Pulteney Street
Geneva, New York 14456, USA