### Introduction to Programming Using Java
Version 9, Swing Edition, 2022

## Preface

Introduction to Programming Using Java is a free introductory computer programming textbook that uses Java as the language of instruction. This version of the book covers Java 17. 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 textbook home page, https://math.hws.edu/javanotes-swing/, has links for downloading both web site and PDF versions of the book. Example programs and solutions to end-of-chapter exercises are part of the web site download and can also be downloaded separately. 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 more advanced features.

Java currently has two major approaches to Graphical User Interface programming: JavaFX and Swing. This edition of the textbook uses Swing, but there is an alternative edition that uses JavaFX. The main differences are in Chapters 6 and 13, which are devoted to GUI programming, but GUI programs and GUI-related material in other chapters also use Swing exclusively. For the Swing edition, much of the GUI material is taken from Version 7 of this textbook, with some updating and modification.

Swing is a standard part of Java. JavaFX was introduced as a more modern approach to GUI programming, but it must be downloaded and installed separately from Java itself, which makes it more complicated to use. Swing and JavaFX can both be used to write complex, fully functional GUI programs, and either one is a reasonable choice. Version 8 of this textbook used JavaFX. The alternative edition that uses Swing has been added for Version 9.

GUI programming was never included in the textbook as an end in itself, and it would take another textbook to cover the topic in its entirety. I cover GUI because it is a great example of object-oriented programming, it lets me introduce event-driven programs, and it lets students literally see the effect of the code that they write. JavaFX and Swing both offer good support for all of those purposes.

Version 8 of this textbook originally covered Java 8, but minor updates of that version added notes about new features in Java 9 through Java 16. Version 9 of the book covers Java 17. The main change from Version 8 is the addition of the Swing edition. A section on records has been added to Chapter 7. Many examples have been modified to use text blocks and the new switch statement syntax. Some references to the general idea of "abstraction" have been added, such as a short subsection on control abstraction in Section 3.1. A short subsection on final classes and methods has been added to Section 5.5. There are also small corrections and modifications throughout.

The majority of this textbook is valid for Java 8, and the book does not cover features added after Java 8 in as much detail. Note that only Java 8, 11, and 17 are "long-term support" releases. When I introduce a feature that requires Java 11 or Java 17, I will make note of that fact. However, I will never refer to any of the non-long-term-support releases.

The first version of the book was written in 1996, and there have been several versions since then. Version 9 will be the last version. All editions are archived (at least until my retirement in December 2022 and hopefully beyond) at the following Web addresses:

- First edition: https://math.hws.edu/eck/cs124/javanotes1/ (Covers Java 1.0.)
- Second edition: https://math.hws.edu/eck/cs124/javanotes2/ (Covers Java 1.1.)
- Third edition: https://math.hws.edu/eck/cs124/javanotes3/ (Covers Java 1.1.)
- Fourth edition: https://math.hws.edu/eck/cs124/javanotes4/ (Covers Java 1.4.)
- Fifth edition: https://math.hws.edu/eck/cs124/javanotes5/ (Covers Java 5.0.)
- Sixth edition: https://math.hws.edu/eck/cs124/javanotes6/ (Covers Java 5.0, with a bit of 6.0.)
- Seventh edition: https://math.hws.edu/eck/cs124/javanotes7/ (Covers Java 7.)
- Eighth edition: https://math.hws.edu/eck/cs124/javanotes8/ (Covers Java 8, plus a bit of 11 and 17.)
- Ninth edition with JavaFX: https://math.hws.edu/eck/cs124/javanotes9/ (Covers Java 17.)
- Ninth edition with Swing: https://math.hws.edu/eck/cs124/javanotes9-swing/ (Covers Java 17.)

This textbook is free, but it is not in the public domain. Version 9 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.

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 MacOS. I have made the complete source files available for download here:

https://math.hws.edu/eck/cs124/downloads/javanotes9-swing-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.

The source files for the JavaFX edition and for the Swing edition are actually generated from a common set of source files that would be even harder to use. If you are interested in exploring them, those ultimate source files can be found on Github. You can browse the files online, or you can clone the git repository, using the following URL:

https://github.com/davidjeck/javanotes9

Professor David J. Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Email: eck@hws.edu
WWW: https://math.hws.edu/eck/

David Eck