Version 9, JavaFX Edition
May, 2022
David J. Eck
Hobart and William Smith Colleges
This is a PDF version of a free on-line book that is available at
https://math.hws.edu/javanotes/. The PDF does not include
source code files, solutions to exercises, or answers to quizzes, but
it does have external links to these resources, shown in blue.
The PDF also has internal links, shown in red. These links can
be used in Acrobat Reader and some other PDF reader programs.
, ii
©1996–2022, David J. Eck
David J. Eck ()
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456
This book can be distributed in unmodified form for non-commercial purposes.
Modified versions can be made and distributed for non-commercial purposes
provided they are distributed under the same license as the original. More
specifically: This work is licensed under 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/. Other uses require
permission from the author.
The web site for this book is: https://math.hws.edu/javanotes
,Contents
Preface xi
1 The Mental Landscape 1
1.1 Machine Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Asynchronous Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 The Java Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Building Blocks of Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Object-oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.6 The Modern User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.7 The Internet and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Quiz on Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Names and Things 19
2.1 The Basic Java Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.2.2 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2.3 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2.4 Strings and String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.5 Variables in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3 Objects and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3.1 Built-in Subroutines and Functions . . . . . . . . . . . . . . . . . . . . . . 30
2.3.2 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3.3 Operations on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3.4 Text Blocks: Multiline Strings . . . . . . . . . . . . . . . . . . . . . . . . 36
2.3.5 Introduction to Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.4 Text Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.4.1 Basic Output and Formatted Output . . . . . . . . . . . . . . . . . . . . . 39
2.4.2 A First Text Input Example . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.3 Basic TextIO Input Functions . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.4.4 Introduction to File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.4.5 Other TextIO Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.4.6 Using Scanner for Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.5 Details of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.5.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5.2 Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5.3 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.5.4 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
i
, CONTENTS ii
2.5.5 Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.5.6 Assignment Operators and Type Conversion . . . . . . . . . . . . . . . . 53
2.5.7 Precedence Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.6 Programming Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.6.1 Getting a JDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.6.2 Command Line Environment . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.6.3 Eclipse IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6.4 BlueJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.6.5 The Problem of Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.6.6 About jshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.6.7 JavaFX on the Command Line . . . . . . . . . . . . . . . . . . . . . . . . 66
2.6.8 Using JavaFX in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Exercises for Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Quiz on Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3 Control 75
3.1 Blocks, Loops, and Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.1.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.1.2 The Basic While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.1.3 The Basic If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.1.4 Control Abstractiont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.1.5 Definite Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.2 Algorithm Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.2.1 Pseudocode and Stepwise Refinement . . . . . . . . . . . . . . . . . . . . 83
3.2.2 The 3N+1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.2.3 Coding, Testing, Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.3 while and do..while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.3.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.3.2 The do..while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.3.3 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.4.1 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.4.2 Example: Counting Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.4.3 Nested for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.5 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
3.5.1 The Dangling else Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.5.2 Multiway Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.5.3 If Statement Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.5.4 The Empty Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.6 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3.6.1 The Basic switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3.6.2 Menus and switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 115
3.6.3 Enums in switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.6.4 Definite Assignment and switch Statements . . . . . . . . . . . . . . . . . 117
3.6.5 Switch Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
3.6.6 The Traditional switch Statement . . . . . . . . . . . . . . . . . . . . . . 118
3.7 Exceptions and try..catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120