Object-Oriented Design & Patterns

Cay S. Horstmann

Chapter 1

A Crash Course in Java

Chapter Topics

"Hello, World" in Java

"Hello, World" in Java

"Hello, World" in Java

Using the SDK

Note:  You must have the java binaries in your $PATH
environment variable. Instructions for Windows (XP/2000) and for Unix.

Using the SDK


Using BlueJ

Using BlueJ


Documentation Comments

Documentation Comments - Summary


Documentation Comments - Detail


Documentation Comments

Documentation Comments - API Docs


Primitive Types

Simple Application: compute factorial

/** Computes and displays n! */
public class Factorial {

    /** main function
      * @param args the command line arguments, stored in a String array
      */
    public static void main(String[] args) {
        int f = 1;

        // convert command line argument to integer type:
        int n = Integer.parseInt(args[0]);

        // calculate factorial
        while (n > 1) {
            f = f * n;
            n--;
        }

        // display result:
        System.out.println(args[0] + "! = " + f);
    }
}


Read section "Language Basics" from the Java Tutorial,
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

Variables

A variable is an item of data named by an identifier.
The variable's type determines what values it can hold
and what operations can be performed on it.

Variable declaration:

type name
Variable scope: section of code where the variable's simple name can be used.
The scope is determined implicitly by the location of the variable declaration.

Scope Categories:

Example:

  class Myclass {
    private int p;

    // ....
    public void method() {
       int i;
       for (i=0; i<10; i++) {
          int j = i * p;
          // ...
       }

       System.out.println("i=" + i);
       System.out.println("j=" + j);
     }
    }
   }

Variable initialization:

With assignment statement:
    float pi = 3.1415;

Final Variables:

The value of a final variable cannot change after it has been initialized.
Are similar to constants.
    final float e = 2.71828183;

Primitive data types:

Keyword Description Size/Format
(integers)
byte Byte-length integer 8-bit two's complement
short Short integer 16-bit two's complement
int Integer 32-bit two's complement
long Long integer 64-bit two's complement
(real numbers)
float Single-precision floating point 32-bit IEEE 754
double Double-precision floating point 64-bit IEEE 754
(other types)
char A single character 16-bit Unicode character
boolean A boolean value (true or false) true or false

Reference type:

The value of a reference type variable, in contrast to that of a primitive type,
is a reference to (an address of) the value or set of values represented by the
variable:


Arrays, classes, and interfaces are reference types.

    Greeter g1 = new Greeter("Students");

    Greeter g2 = g1;

    int v[] = new int[100]; 

Operators

( http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html) Most operators look similar to C++ operators.

Expressions, Statements, and Blocks

Read from the Java Tutorial, at
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html


Control Flow Statements

Read from the Java Tutorial, at
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html

Control-Flow Statements
Statement Type Keyword
looping while, do-while, for
decision making if-else, switch-case
exception handling try-catch-finally, throw
branching break, continue, label:, return

Control Flow

Object References

More About Objects


The null Reference

The this Reference

Deallocation of Java Objects - The Garbage Collector


Parameter Passing

No Reference Parameters

Packages

Packages

Importing Packages

Importing Packages

Packages and Directories

Exception Handling

Checked and Unchecked Exceptions

Declaring Checked Exceptions

Catching Exceptions

The finally Clause

Strings

Strings

String concatenation

Converting Strings to Numbers

Reading Input

The ArrayList<E> class

The ArrayList<E> class

The ArrayList<E> class

Linked Lists

List Iterators

List Iterators

Arrays

Arrays

Arrays

Command-Line Arguments

Static Fields

Static Methods

Programming Style: Case Convention

Programming Style: Property Access

Programming Style: Braces

Programming Style: Fields

Programming Style: Miscellaneous