Table of Contents

Foundational Java – Table of Contents

1 The Java Story

1.1 A Brief History of Java

1.2 Characteristics of Java

1.2.1 Simple

1.2.2 Object-Oriented

1.2.3 Distributed

1.2.4 Robust

1.2.5 Secure

1.2.6 Architecture Neutral

1.2.7 Portable

1.2.8 High Performance

1.2.9 Multithreaded

1.2.9 Dynamic

1.3 The JDK and the JRE

1.3.1 Java Versions

1.4 Java APIs

1.5 Summary

 

2 Compiling and Running Java Programs

2.1 Java from the Command Line

2.1.1 Setting the Path to the JDK

2.2 A First Java Program

2.2.1 The MyJavaProgram Class

2.2.2 The ‘main’ Method

2.2.3 Output with the System Class

2.3 Compiling Java

2.4 Running Java

Exercise 2.1

2.5 The Java Classpath

2.5.1 Packages

2.5.2 Compiling into a Package Folder

2.6 Comment Syntax

Exercise 2.2

2.7 Using the Eclipse Integrated Development Environment (IDE)

2.7.1 Creating a New Project

2.7.2 Editing Code in Eclipse

2.7.3 Compiling and Running Code in Eclipse

Exercise 2.3

2.8 Summary

 

3 Data Types, Arithmetic and Arrays

3.1 Java Primitive Data Types

3.1.1 Literals and Variables

3.1.2 Literals and the Compiler

3.1.3 Boolean Variables

3.1.4 Unicode Characters and Escape Sequences

3.1.5 Variable Scope

Exercise 3.1

3.2 Arithmetic and Other Operators

3.2.1 Arithmetic Statements

3.2.2 Increment and Decrement Operators

3.2.3 Prefix and Postfix Operators

3.2.4 Assignment Operators

3.2.5 Promotion and Type Casting

3.2.6 String Concatenation

3.2.7 Bitwise Operators

Exercise 3.2

3.3 Arrays

3.3.1 Declaring Arrays

3.3.2 Initialising Arrays

3.3.3 Accessing Array Data

3.3.4 The Array ‘length’ Field

3.3.5 Multidimensional Arrays

3.4 Precedence, Casting and Promotion

Exercise 3.3

3.5 Summary

 

 

4 Control Structures

4.1 Making Selections        

4.1.1 ‘if’ Statements           

4.1.2 Expressing Conditions with Relational and Boolean Operators      

4.1.3 Using Selection: The CoinExample Class

4.1.4 The DieExample class

Exercise 4.1          

4.1.5 Writing Test Code

4.1.6 ‘switch’ Statements

4.1.6.1 A ‘switch’ example - The Dice Man

4.1.6.2 ‘break’ and ‘continue’

Exercise 4.2

4.1.7 The Ternary Operator

4.2 Iteration

4.2.1 ‘while’ and ‘do...while’ Loops

4.2.1.1 A ‘do…while’ Example

4.2.2 ‘for’ Loops

4.2.2.1 Iteration with a ‘for’ loop

4.2.2.2 ‘for’ loops and arrays

4.2.2.3 Iterating through the array passed to ‘main’

4.2.2.4 Multiple Initializations and Actions

Exercise 4.3

4.3 Summary

 

 

5 Creating Objects

5.1 Classes and Objects

5.1.1 Object Creation - Constructors

5.2 Classes and Methods for String Data

5.2.1 The String Class

5.2.2 StringBuilders

5.2.3 StringBuffers

5.2.4 The ‘toString’ Method

Exercise 5.1

5.3 References and Memory

5.3.1 Garbage Collection

5.3.2 Object Equality

5.3.3 String Concatenation and Memory

5.4 Java Library Packages

5.4.1 Importing Classes

5.4.2 Wild Cards and Multiple Imports

5.4.3 Packages and Sub Packages

Exercise 5.2

5.5 Using Javadoc

5.5.1 Viewing Javadoc in Eclipse

Exercise 5.3

Exercise 5.4

5.7 Summary

 

6 Creating Domain Classes

6.1. Object Orientation and Domain Objects

6.1.1 Clock Objects in the Real World

6.1.2 Encapsulation and Information Hiding

6.1.3 Object Identity

6.2 Creating New Classes

6.2.1 Adding Fields to a Class

6.2.2 Adding Methods to a Class

6.3 Constructors

6.3.1 Overloaded Constructors

6.3.2 Chaining Constructors

6.3.3 Internal Encapsulation in Constructors

6.4 Access Modifiers

6.5 Javadoc and Code Comments

6.5.1 Creating Javadoc in Eclipse

6.5.2 Running Javadoc from the Command Line

Exercise 6.1

6.6 Types of Method

Exercise 6.2

Exercise 6.3

6.7 Object Reuse

6.8 Static Fields and Methods

6.8.1 Static Methods

6.8.2 Invoking Static Methods

6.8.3 Static Final Fields

6.8.4 Static Methods in Java Library Classes

Exercise 6.4

6.9 Objects as JavaBean Components

6.9.1 Reflection

Exercise 6.5

Exercise 6.6

Exercise 6.7

6.10 Summary

 

7 Objects Working Together: Association, Aggregation and Composition

7.1 Aggregations as Collections

7.2 Message passing

7.2.1 Object Association

7.2.2 Association Direction

7.2.3 Implementing Associations

7.3 Associating objects: The Snakes and Ladders Game

7.3.1 The Snakes and Ladders Game Source Code

7.3.1.1 Package visibility

7.3.1.2 The Snake and Ladder classes

7.3.1.3 The BoardSquare class

7.3.1.4 The GameBoard class

7.3.1.5 The SnakesAndLadders class

7.3.2 When to Create Objects

Exercise 7.1

7.4 Association, Aggregation or Composition?

7.4.1 Aggregation Example

Exercise 7.2

7.5 Composition

7.5.1 Electronic Gates

7.5.2 Half Adder Components

Exercise 7.3

7.6 Summary

 

 

8 Inheritance, Polymorphism and Interfaces

8.1 Abstraction

8.1.1 Inheritance

8.1.2 Polymorphism

8.1.3 The Relationship between Inheritance and Polymorphism

8.2 Inheriting from the Object Class

8.2.1 Overriding the ‘toString’ Method

8.2.2 Overriding the ‘equals’ Method

8.2.3 Overriding the ‘hashCode’ Method

8.2.4 The @Override Annotation

Exercise 8.1

8.3 An Inheritance Hierarchy Using Abstraction

8.3.1 Abstract Classes

8.3.2 Protected Visibility and Inheritance

8.3.3 Creating Subclasses

8.3.4 Calling Superclass Constructors

Exercise 8.2

8.3.5 Adding Further Subclasses

Exercise 8.3

8.4 Dynamic Binding

8.4.1 Using Polymorphic Methods

Exercise 8.4

8.5 Interfaces

8.5.1 Naming Interfaces

8.5.2 Implementing an Interface

Exercise 8.5

Exercise 8.6

Exercise 8.7

Exercise 8.8

8.6 Summary

 

9 Exception Handling

9.1 Java Exceptions

9.1.1 The Exception Handling Hierarchy

9.1.2 Checked and Unchecked Exceptions

9.1.3 Exception Handling Keywords

9.2 Handling Checked Exceptions

9.2.1 Catching Exceptions - ‘try’ and ‘catch’

9.3 Handling Unchecked Exceptions

9.3.1 Exiting

9.4 Catching Multiple Exceptions

9.4.1 Ordering Multiple ‘catch’ Blocks

Exercise 9.1

9.5 Throwing an Exception with ‘throw’

9.5.1 Delegating Responsibility

Exercise 9.2

9.6 Writing and Using Custom Exceptions

9.6.1 Throwing a Custom Exception

9.6.2 ‘finally’ Blocks

Exercise 9.3

9.6.3 Re-throwing Custom Exceptions

Exercise 9.4

Exercise 9.5

9.7 Summary

 

10 Unit Testing With JUnit

10.1 The JUnit Test Framework

10.1.1 Using JUnit with Eclipse

10.2 Test Cases and Units Under Test

10.2.1 Creating a JUnit Test Case

10.2.2 Required Imports

10.2.3 Writing Test Methods

10.2.4 Using TestRunners

10.2.5 The Eclipse TestRunner

Exercise 10.1

10.3 Types of Assertions

10.3.1 Asserting Floating Point Equality

10.3.2 Object Equality

10.3.3 Other Assertions

Exercise 10.2

10.4 Exceptions, Timeouts and Failures

10.4.1 Forcing Failures

10.5 Arranging a Test with @Before and @After Methods

10.5.1 The 3A Pattern

10.5.2 @BeforeClass and @AfterClass Methods

Exercise 10.3

10.6 Writing a Test Suite

Exercise 10.4

10.7 Summary

 

11 Exploring the Java Libraries

11.1 Frequently Used Classes in the java.lang Package

11.1.1 The java.lang.Object Class

11.1.1.1 The ‘finalize’ method

11.1.1.2 The ‘clone’ method

Exercise 11.1

11.1.2 The java.lang.Math Class

Exercise 11.2

11.1.3 The java.lang.System Class

11.1.4 Wrapper Classes

11.2 Classes in the java.util Package

11.2.1 The Date class

11.2.2 The Calendar Class and Factory Methods

11.3 Formatter Classes in the java.text Package

11.3.1 Formatting Dates

11.3.2 Parsing Dates

Exercise 11.3

11.3.3 Formatting and Parsing Numbers

11.3.3.1 The Number class

11.3.4 Formatting and Parsing Currency

11.3.4.1 Handling different currencies

Exercise 11.4

Exercise 11.5

11.4 Summary

 

12 The Collections Framework and Generics

12.1 Objects That Contain Objects

12.1.2 Associations and Collections

12.2 The Core Collection Interfaces

12.2.1 Partial Implementations of the Core Interfaces

12.2.2 Concrete Implementations of the Core Interfaces

12.2.3 Legacy Classes

12.3 Typesafe Collections with Generics

12.4 A Concrete Collection Class – The ArrayList

12.4.1 Adding and Retrieving Objects

12.4.2 Wrapping and Autoboxing

12.4.3 Iterators

12.4.4 ListIterator

12.4.5 Enhanced ‘for’ loop

12.4.6 An ArrayList of Module objects

Exercise 12.1

12.5 Maps

12.5.1 Map Views

12.5.2 Using a TreeMap

Exercise 12.2

Exercise 12.3

Exercise 12.4

12.6 Utility Classes

12.6.1 Comparing Objects with the Comparable Interface

12.6.2 The Collections class

12.6.3 Creating a Comparator

Exercise 12.5

Exercise 12.6

Exercise 12.7

Exercise 12.8

12.7 Generics and inheritance

12.7.1 Specifying bounds

Exercise 12.9

12.8 Summary

 

13 Input and Output Streams

13.1 Java Stream Classes

13.1.1 Different Types of Stream

13.2 Byte Streams

13.2.1 Streaming Bytes To and From Files

13.3 Filter Streams

Exercise 13.1

13.4 Readers and Writers

13.4.1 Buffered Readers and Writers

13.5 PrintStreams and PrintWriters

13.6 The File Class

13.7 Streaming Objects

13.7.1 Serializing Course Objects

Exercise 13.2

13.8 The New IO Library

13.8.1 Buffer Classes

13.8.1.1 ByteBuffers

13.8.2 File Channels

13.8.2.1 Reading from a Channel

13.8.3 View Buffers

13.8.4 Charsets and String Data

Exercise 13.3

13.9 Summary

 

14 Automatic Building and Testing with Ant

14.1 Installing and Configuring Ant

14.1.1 Using Ant in Eclipse

14.2 The Ant Build File – ‘build.xml’

14.2.1 Ant Properties

14.2.2 Tasks and Targets

14.2.3 Running an Ant Build File in Eclipse

Exercise 14.1

14.2.4 Packaging Code with the Ant ‘jar’ Task

14.2.5 Target Dependencies

14.3 Running Code, Forking and Classpaths

Exercise 14.2

14.3.1 Setting the Classpath with a ‘path’ Element

14.4 Running Tests

14.4.1 Running Tests with the ‘junit’ and ‘test’ Elements

14.4.2 Running Multiple Tests with the ‘batchtest’ Element

Exercise 14.3

14.5 Summary

 

15 Java and the Database (JDBC)

15.1 An Example Database

15.2 Using MySQL

15.2.1 Creating a New Database

15.2.2 Generating Primary Keys

15.2.3 Using a DDL Script

15.2.4 Viewing Table Schema

15.2.5 Creating an Authorized MySQL User

15.3 Java Database Access with JDBC

15.3.1 JDBC Drivers

15.3.2 Making a Two Tier Connection

15.4 SQL Exceptions

15.5 Executing SQL Queries and Updates Using JDBC

15.5.1 Processing ResultSets

Exercise 15.1

Exercise 15.2

15.5.2 Updating Records

Exercise 15.3

15.6 Using Prepared Statements

Exercise 15.4

15.7 Summary

 

16 Multithreading

16.1 Creating and Running a Thread

16.1.1 Thread States

16.2 Running Multiple Threads

Exercise 16.1

16.3 Thread Priority

16.3.1 Setting Thread Priority

Exercise 16.2

16.3.2 Yielding

16.4 Implementing the Runnable Interface

Exercise 16.3

Exercise 16.4

16.5 Synchronizing Threads

16.5.1 Monitors and Synchronization

16.5.2 ‘wait’, ‘notify’ and ‘notifyAll’

16.6 Synchronized Code Example

16.6.1 The Sensor Class

16.6.2 The Analyzer Class

Exercise 16.5

16.7 Concurrent collections

16.8 Summary

 

17 Building GUIs with the JFC Swing Library

17.1 Components. Containers and Frames

17.2 Creating a Main Window Frame

17.2.1 Setting the JFrame’s Title

17.2.2 Selecting the JFrame’s Closing Behaviour

17.2.3 Sizing the JFrame

17.2.4 Showing the JFrame

17.3 Swing Component Classes

17.3.1 The JLabel class

17.3.2 Adding Components to a Frame

17.3.3 Manually Positioning and Sizing Components

17.3.4 Common Methods of Component and JComponent

17.3.5 The JTextField Class

17.3.6 The JButton Class

17.3.7 Placing Components using ‘setBounds’

Exercise 17.1

17.4 Colors, Fonts and Icons

17.4.1 Setting Colors

17.4.2 Setting Fonts

17.4.3 Font Style and Size

17.4.4 Icons on Labels and Buttons

Exercise 17.2

17.5 Some Additional Components

17.5.1 JTextArea

17.5.2 Check Boxes and Radio Buttons

17.5.3 JComboBox

17.5.4 JSlider

17.5.5 JPanel

17.6 Setting the Look and Feel

Exercise 17.3

Exercise 17.4

17.7 Layout Managers

17.7.1 BorderLayout

17.7.1.1 BorderLayout constraints

17.7.2 FlowLayout

17.7.2.1 Adding a FlowLayout JPanel to a JFrame

17.7.2.2 FlowLayout constraints

17.7.3 GridLayout

17.8 Summary

 

18 Event Driven Programming

18.1 Event Listeners

18.2 Event Classes

18.3 EventListener Interfaces

18.3.1 A JButton ActionListener

18.3.2 Adding a Listener

18.4 Multiple Action and Focus Listeners

Exercise 18.1

Exercise 18.2

18.5 Responding to Mouse Events

18.5.1 Mouse Listener Events

18.5.2 Mouse Motion Events

Exercise 18.3

Exercise 18.4

18.6 Event Handlers as Inner Classes

18.6.1 Implementing a DieListener

18.6.2 Inner Classes

18.6.3 Local Inner Classes

18.6.4 Anonymous Inner Classes

18.7 JPanel Subclasses and Multi Panel Layouts

18.7.1 The GridBagLayout

18.7.1.1 gridx and gridy

18.7.1.2 gridwidth and gridheight

18.7.1.3 fill

18.7.1.4 insets

18.7.1.5 ipadx and ipady

18.7.1.6 anchor

18.7.1.7 weightx and weighty

18.8 Separating the ‘Model’ from the ‘View’

18.8.1 The Model Class: FlightDistances

18.8.2 The View Class: DistanceViewer

Exercise 18.5

Exercise 18.6

18.9 Summary

 

 

19 Dialogs and Menus, Models and Views

19.1 Predefined Dialogs in Swing

19.1.1 Message Box Dialogs using JOptionPane

19.1.2 File Chooser Dialogs using JFileChooser

Exercise 19.1

19.1.3 Color Chooser Dialogs using JColorChooser

19.2 Custom Dialogs with JDialog

Exercise 19.2

19.3 Using Menus

19.3.1 Adding a Menu to a Frame

19.3.2 Menu Enhancement: Separators, Mnemonics and Accelerators

19.3.2.1 Menu separators

19.3.2.2 Mnemonics

19.3.2.3 Keyboard accelerators

19.3.3 MenuListeners

Exercise 19.3

19.4 Model View Controller in Swing Components

19.4.1 Model and View in JTextPane Components

19.4.1.1 Adding a Document

19.4.1.2 Text Styling with AttributeSets

19.4.1.3 Presenting read-only text

Exercise 19.4

Exercise 19.5

19.4.2 Model and View in the JTable Component

19.4.2.1 Implementing the TableModel interface

19.4.2.2 Creating a JTable view

Exercise 19.6

Exercise 19.7

19.5 Summary

 

20 Java Web Start and Applets

20.1 Web Browsers, URLs and HTML Pages

20.1.1 URLs

20.1.1.1 The HTTP protocol

20.1.1.2 The server address

20.1.1.3 The path and resource name

20.1.2 HTML (HyperText Markup Language)

20.2 Java Web Start

20.2.1 Creating a JAR in Eclipse

20.2.2 Java Network Launch Protocol (JNLP)

20.2.3 Deploying to a server

20.2.3.1 Installing and starting Tomcat

20.2.3.2 The ‘localhost’ URL and port number

20.2.3.3 Web application structure and deployment

20.2.3.4 XML deployment descriptors

20.2.3.5 Setting the ‘codebase’ and ‘href’ in the JNLP file

20.2.3.6 Deploying to Tomcat

20.2.3.7 Deploying the WAR file with Ant

20.2.4 Running Applications from the Cache

20.2.5 Security

Exercise 20.1

20.3 Applets

20.3.1 How Applets Differ from Applications

20.3.2 Running an Applet in the Applet Viewer

Exercise 20.2

20.4 Summary

Comments