Java Programming
William W. Provost
Student Guide
Revision 1.1
Copyright © 1999 William W. Provost
All Rights Reserved
Java Programming
Student Guide
Information in this document is subject to change without notice. Companies, names and data
used in examples herein are fictitious unless otherwise noted. No part of this document may be
reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Object Innovations.
Copyright ©1999 William W. Provost All rights reserved.
Object Innovations, Inc.
420 Boston Turnpike
Shrewsbury, MA 01545
(508) 845-1195
[Link]
Printed in the United States of America.
Java, JavaSoft and Java Development Kit are trademarks of Sun Microsystems. Microsoft,
Windows, Windows NT, Windows 95 and Windows 98 are either trademarks or registered
trademarks of Microsoft Corporation in the United States of America and other countries.
Rev. 1.1 Copyright © 1999 William W. Provost ii
All Rights Reserved
Table of Contents
Course Overview
Module 1. The Java Software Environment
Chapter 1 The Java Virtual Machine
Chapter 2 Building Java Software
Module 2. The Java Language
Chapter 1 Basics
Chapter 2 Data Types
Chapter 3 Flow Control
Chapter 4 Threads
Module 3. Object-Oriented Programming in Java
Chapter 1 Object-Oriented Software
Chapter 2 Encapsulation in Java
Chapter 3 Inheritance and Polymorphism in Java
Chapter 4 Inner Classes
Module 4. Java Graphical User Interfaces
Chapter 1 The Abstract Windowing Toolkit
Chapter 2 Layout Management
Chapter 3 Events
Chapter 4 Applets
Module 5. Java Streams
Chapter 1 The Java Streams Model
Chapter 2 File I/O
Chapter 3 Object Serialization
Appendix A Java Documentation
Appendix B The Car Dealership Case Study
Appendix C Learning Resources
Rev. 1.1 Copyright © 1999 William W. Provost iii
All Rights Reserved
Module 1. Java Software Environment
• Chapter 1. Java Virtual Machine
− This chapter introduces the fundamentals of the Java
architecture, including compile and interpret steps, the Java
Virtual Machine, and the Core API.
− It is suitable for non-programmers as a brief introduction to
the features of the architecture.
• Chapter 2. Building Java Software
− This chapter goes deeper into the stages of the development
process for Java software, and gives the first look at the
structure of Java source code, and a more detailed look at
JDK command-line tools.
− It also covers use of Java ARchive files, or JARs.
− It concludes with an introduction to the Java security model.
Rev. 1.1 Copyright © 1999 William W. Provost iv
All Rights Reserved
Module 2. The Java Language
• Chapter 1. Basics
− This chapter starts a ground-up process of understanding the
elements of the Java language itself.
− Fundamental grammar and reserved words are covered.
− Also the most basic elements of the Core API are introduced.
• Chapter 2. Data Types
− This chapter introduces the Java data types, including
primitive numeric types, characters, booleans, object
references, Strings and arrays.
• Chapter 3. Flow Control
− This chapter covers the various means of controlling flow of
execution in Java, in particular conditional expressions and
loops.
− Exceptions and exception handling are also covered.
• Chapter 4. Threads
− This chapter covers the use of threads in the Java Virtual
Machine.
− It also looks at the mapping from those threads to Java
objects that model them programmatically.
− Thread synchronization is also addressed.
Rev. 1.1 Copyright © 1999 William W. Provost v
All Rights Reserved
Module 3. OO Programming in Java
• Chapter 1. Object-Oriented Programming
− This course gives an overview of object-oriented analysis and
design concepts and methodology.
− It is appropriate for non-programmers, but is primarily there
to lead programmers without much OO experience into the
remainder of the module.
− Programmers with moderate to extensive OO experience may
wish to skip this chapter entirely.
• Chapter 2. Encapsulation
− This chapter covers the use of the Java class construct to
encapsulate relevant concepts in software.
• Chapter 3. Inheritance and Polymorphism
− This chapter moves beyond encapsulation to look at the Java
class inheritance model, including overriding of methods to
achieve polymorphism through object references.
− Interfaces and abstract classes are also treated.
• Chapter 4. Inner Classes
− This chapter focuses on an OO construct unique to the Java
language, known as inner classes.
− The motivation for, and some examples of, inner classes are
covered.
Rev. 1.1 Copyright © 1999 William W. Provost vi
All Rights Reserved
Module 4. Java GUIs
• Chapter 1. The Abstract Windowing Toolkit
− This chapter introduces the Abstract Windowing Toolkit
(AWT), a part of the Core API designed to model windowing
and graphics functionality in a portable way.
− Topics covered include windows, dialogs, control types, and
menus.
• Chapter 2. Layout Management
− This chapter deals with the AWT’s unique approach to visual
layout of child windows and controls.
− Both use of standard layout managers and development and
use of custom layout managers are covered.
• Chapter 3. Events
− This chapter covers the Java event model, which is directly
relevant to AWT for user-driven event handling, but also
extends beyond AWT to generalize the concepts of an event,
a source, and a listener or handler.
− The chapter covers both standard and custom event types.
• Chapter 4. Applets
− Here we look at the specific requirements for developing
Java applets, as opposed to applications.
− We look at a sample applet, its reference from an HTML
page, and its use in a browser or the applet viewer.
Rev. 1.1 Copyright © 1999 William W. Provost vii
All Rights Reserved
Module 5. Java Streams
• Chapter 1. The Java Streams Model
− This chapter introduces the Java streams model, a highly
flexible and extensible delegation-based model for defining
stream classes of a particular featureset.
• Chapter 2. File I/O
− Here we drill down on one major set of media-based stream
classes: those modeling file input and output.
• Chapter 3. Object Serialization
− This chapter provides an introduction to the Java
Serialization API, which through the use of Java Reflection
to read object type information at runtime can almost
completely automate the process of saving or loading an
object or complex graph of related objects.
− Basic usage is covered, and at the end of the chapter we look
at some hooks by which the process can be customized.
Rev. 1.1 Copyright © 1999 William W. Provost viii
All Rights Reserved
Prerequisites
• In order to get the most out of this course, it is
expected that students have some experience
programming in a high-level language.
− Students with experience in C or C++ will probably have the
easiest time adjusting to the Java language.
− But any programming experience will be useful, and there are
many facets of the Java architecture that must be learned
from the ground up regardless of a student’s knowledge of
other languages.
− This course does not attempt to cover basic elements of
software development; it is assumed that students are
familiar with terms such as executable, library, file system,
source code, compilation, flow control, CPU, thread,
network, variable, algorithm, and so on.
• No prior experience with Java itself is assumed.
Rev. 1.1 Copyright © 1999 William W. Provost ix
All Rights Reserved
Conventions
• Particularly since many students will be learning a
language that may seem deceptively familiar, we
attempt to highlight concepts that may seem
straightforward, but can be treacherous.
− We call these “gotchas.”
− Where such a concept is presented you will see the icon at the
left.
• Also, there are many possible paths through this or
any Java courseware, and although the course is
written to accommodate as many of those as
reasonable, we recognize one important dichotomy.
− Students with extensive experience in C++ programming and
development are closest to understanding Java,
grammatically and architecturally, from the start.
− This can be a blessing and a curse, since many Java
constructs only look like C++ but will not behave as
expected; they are false cognates.
− However, much of the material in this course will be pure
review for some students, so we have flagged such material
on a page-by-page basis, with the icon at left.
− Classes composed of experienced C++ programmers should
feel free to skim or skip this material, at their and their
instructor’s discretion.
Rev. 1.1 Copyright © 1999 William W. Provost x
All Rights Reserved
A Portable Format?
• Owing to the nature of Java, this course is not
platform specific.
• All the lab software is 100% Pure Java and will port
to any Java Runtime Environment version 1.2 or
higher. Any JDK version differences will be
highlighted with the icon at left.
• There are however some practical points which we
will need to illustrate using one operating system as
the course standard.
• In particular, when dealing with directory and file
paths, we will use the reverse slashes common to
Microsoft Windows operating systems.
− There are some places in which paths will be interpreted by
Java software such that the forward slash will be mapped to
the target OS.
− Thus we use the platform-specific syntax where it would be
needed on Windows systems, to distinguish the two cases.
• Also in the few places in which console commands
other than invocations of JDK tools must be
illustrated, we will use Windows/DOS commands as a
reference.
Rev. 1.1 Copyright © 1999 William W. Provost xi
All Rights Reserved
Labs
• The course relies heavily on hands-on experience in
various topics and techniques.
• Lab software is written entirely in Java.
• Where possible, starter code is provided to take work
off the student’s hands that would be largely
irrelevant to the topic of the lab; thus the student can
be as productive as possible in the time allotted and
focus on the topic at hand.
• The course software is deployed in several main
directories off of the course root directory.
− The Examples directory holds code examples, including all
the starting, intermediate and answer versions of all the labs,
with HTML documentation.
− The Labs directory holds one subdirectory for each lab in the
course, named for the course module and lab number. This is
where you will do the bulk of your lab work, with some
reference to code in the Examples directory.
− The Classes directory serves as the build target for all Java
compilation, and as the only (necessary) directory in the Java
class path for testing.
− The Demos directory is home to files that relate to in-class
demonstrations, many of which will lead up to further
development in labs.
Rev. 1.1 Copyright © 1999 William W. Provost xii
All Rights Reserved
Lab Instructions and Tools
• In order to keep the course material as portable as
possible between different development toolsets, we
have relied only on the command-line tools in the
JDK for lab work.[WWP1]
• If for your own productivity you choose to use a Java
IDE or other development tool, you are encouraged
to limit the use of that tool to code editing.
− Do your compiling and testing from the command line.
− These tools form the reference implementation of the JDK,
and not all commercial tools, however attractive for other
reasons, support every feature or behave correctly in every
condition.
− This lab software has been thoroughly tested only on the
JavaSoft JDK command-line tools.
Rev. 1.1 Copyright © 1999 William W. Provost xiii
All Rights Reserved
Lab Terminology and Conventions
• Some of these terms will be developed over the first
few labs in this course, but as an introduction and for
later reference, certain instructions are defined
specifically here:
− If you are instructed to compile a class or source file, do so
by typing the following command from the directory that
holds the source code:
javac –d %TargetPath% <classname>.java
− If you are instructed to build a project, do so thusly:
javac –d %TargetPath% *.java
− To run or to test an application:
java <package>.<classname>
• Code examples are sometimes inlined in the
instructions.
• Also commands to be entered in a DOS box or
console/terminal window are sometimes shown
literally (as in the three examples above.)
− Sometimes there must be a mix of commands you are to type
and expected output or feedback from the application or tool.
− In this case your typing will be boldfaced, and the responses
you should receive will be in normal face.
java [Link]
Exception in thread “main”: [Link]: main
Rev. 1.1 Copyright © 1999 William W. Provost xiv
All Rights Reserved
Page: 13
[WWP1]This will change from version to version of the course. We may add a dictionary of terms as
observed in lab instructions and mapped per version to different actions in different IDEs.