Introduction to Programming Using Java
Version 8.1.3, August 2021
Preface
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 more than 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 uses Java 8, with brief coverage of features that were added to the language in Java 9 and later. JavaFX is used for GUI programming. All sample programs and all exercise solutions have been compiled with Java 11 and with Java 16, as well as with Java 8.
The home web site for this book, which is always the latest version, is http://math.hws.edu/javanotes/. 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. And some people believe that object oriented programming should be emphasized from the very beginning. These are 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 became unclear because, as of Java 11, JavaFX is no longer included as part of the basic Java download. However, it is still available to be downloaded and installed separately. People who are interested in using Swing instead of JavaFX might consider using Chapter 6 and Chapter 13 from Version 7 in place of the same chapters from Version 8.) The eighth edition also incorporates two 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.
Version 8.1 was a fairly small update to Version 8.0, and Versions 8.1.1 through 8.1.3 made only very minor changes. The main motivation for the update from 8.0 to 8.1 was to make it clearer how to use the book with Java 11 and later, but I also took the opportunity to briefly discuss a few features that were introduced after Java 8. The major change to the language itself was the introduction of modules in Java 9; these were already mentioned in Version 8.0, but that discussion has been updated. (However, this book does not explain how to create modules.) Aside from that, there have been few language changes that impact this textbook, but Version 8.1 does introduce the use of var for declaring variables (Subsection 4.8.2), several new methods related to the stream API, and the revisions made to the switch statement in Java 14 (Subsection 3.6.5). Version 8.1.2 added brief mentions of two additional new features in Java 15 and 16: text blocks (Subsection 2.3.5) and records (Subsection 7.1.4). It also updated the information about programming environments in Section 2.6. Version 8.1.3 is primarily a further update of the information in Section 2.6.
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 in December 2022) 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, plus a bit of 11 and 17.)
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.
- Use the book as a textbook for a course that you are teaching (even though the students are paying to take that course).
- 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:
http://math.hws.edu/eck/cs124/downloads/javanotes8-full-source.zip
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
Email: eck@hws.edu
WWW: http://math.hws.edu/eck/