Full HTML for

Basic foilset Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts

Given by Nancy J. McCracken,Geoffrey C. Fox at Peking Tutorial, Web Certificate on Spring-Summer 1997. Foils prepared 18 May 97
Outside Index Summary of Material


In Part 1 of the Tutorial We Covered:
  • Overview including History and alpha versus production issues
  • Comparison of Java and Javascript
  • Overall Java Philosophy and Features including security etc.
This Part(2) of Tutorial Covers
Java Programming Language
  • Introduction to Applications,Applets and their Invocation from HTML
  • "Hello World" and Basic Graphics Applets
Object Oriented and Class Structure
  • Methods, Constructors etc.
  • Interfaces
Exceptions
And in the Remaining Parts of the Java Tutorial We Cover:
  • Applet Programming and Threads
  • Abstract Windowing Toolkit
  • Networking and I/O
  • Futures and HPCC Implications

Table of Contents for full HTML of Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts

Denote Foils where Image Critical
Denote Foils where HTML is sufficient
denotes presence of Additional linked information which is greyed out if missing

1 Java Tutorial - Spring 97
Part II: Java Language and
Object-Oriented Concepts
http://www.npac.syr.edu/projects/tutorials/Java/

2 Resources for the Java Programming Language
3 Java in Practice --
Applications
Applets and their use from HTML

4 Java Applications
5 The Simplest Java Application: Hello, World!
6 Java Applets
7 Applet Tag: Calling Applets from HTML - I
8 Applet Tag: Calling Applets from HTML - II
9 <param> Tags and Applets
10 The Simplest Java Applet: Hello, World!
11 Displaying your applet from a Web page.
12 Java Language Basics
13 Java Language -- Lexical Issues I
14 Java Language -- Lexical Issues II
15 Java Language -- Program Structure
16 Java Language -- Variable/Expression Types
17 Java Language -- Types: Array
18 Java Language -- More on Arrays
19 Java Language -- Expressions
20 Java Language -- Control Flow I
21 Java Language -- Control Flow II -- continue
22 Java Language -- Control Flow III -- break and for loop
23 Java Language -- Control Flow IV -- break and switch
24 Java Language -- Control Flow V -- continue and switch
25 Basic Graphics Java Applets
to illustrate language
(more later!)

26 The Graphics class
27 Using Graphics properties
28 Drawing simple shapes
29 Passing parameters to an applet: the HTML
30 Passing parameters to an applet: the applet
31 Centering a string in the applet window.
32 Applet Flow of Control
33 Random color boxes example
34 Arrays of random colors
35 The Java Object Model: Classes, Instances and Methods
36 The Java Object Model Overview
37 API of a Class
38 Using a Class
39 A Computational Class
40 Class Definitions
41 Constructors of Objects
42 Java Language -- Types of Classes - I
43 Java Language -- Types of Classes - II
44 Java Language -- Types of Methods
45 The Java Object Model: Inheritance and the Class Hierarchy
46 Relationships between Classes
47 Use of Methods Defined in Parent
48 Use of Methods Defined in Parent but overridden in child class
49 Comments on Casting
50 Array - A Pseudo Class!
51 By value and By reference
52 Comments on Overloading and Overriding in Classes
53 Object-Oriented
Programming Examples:
Basic Class Operations

54 The Class Hierarchy of this example
55 Object-Oriented Programming in JAVA - defining a class
56 More methods in the class definition
57 Defining a child class
58 Using parent and child classes
59 Parent and child example, continued
60 More child classes
61 Abstract Methods
and Classes
Interfaces
(classes without implementation)

62 Abstract Methods and Classes
63 Java Language -- Interfaces - Overview
64 Cars as an Examples of Interfaces/ Multiple Inheritance
65 Picture of Interfaces and Classes for Cars and their Manufacture
66 Java Language -- Interface Example -- Implementing Storable
67 Interfaces can be used as Classes in type specification
68 Further Features of Interfaces
69 More on Interfaces -- Why use them
70 Packages in Java
71 Overview of Packages and Directory Structure
72 Using Java packages
73 Java 1.0 System Packages
74 More on the Java Language: Exceptions
75 Java Language -- Handling Runtime Errors Using Exceptions
76 User Created Exceptions
77 Basic Structure of Exception Handling in Nested Calls
78 Examples of Exception Hierarchy
79 Example of Handling Exceptions
80 Classes of Exceptions
81 Exceptions in Applets

Outside Index Summary of Material



HTML version of Basic Foils prepared 18 May 97

Foil 1 Java Tutorial - Spring 97
Part II: Java Language and
Object-Oriented Concepts
http://www.npac.syr.edu/projects/tutorials/Java/

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Instructors: Geoffrey Fox ,
Nancy McCracken
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HTML version of Basic Foils prepared 18 May 97

Foil 2 Resources for the Java Programming Language

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The original resource was the The Java Language Specification by Sun Microsystems, Inc., March 1995 updated to October 1995 but superceded by Gosling and Arnold Book
  • Addison Wesley has several other fundamental Java books on Application Programming Interface and Language Specification and Virtual Machine (by end of summer 1996)
http://www.javasoft.com web site has plenty of references including
  • Tutorial: http://www.javasoft.com/books/Series/Tutorial/index.html
  • Books:http://www.javasoft.com/java.sun.com/aboutJavaSoft/book-news.html
  • Collection of Applets: http://www.gamelan.com
Most of the books cited earlier have CDROM's with examples or the JDK.

HTML version of Basic Foils prepared 18 May 97

Foil 3 Java in Practice --
Applications
Applets and their use from HTML

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 4 Java Applications

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
All Java programs are written into a file with a ".java" extension.
Applications are .java files with a main class which is excuted first.
How to compile and run a Java application:
  • Run the compiler on a .java file:
    • javac MyProgram.java
    • producing a file "MyProgram.class" of Java bytecodes
  • Run the interpreter on a .class file:
    • java MyProgram
    • which executes the bytecodes
The resources javac and java are part of JDK and are not in Netscape and so are not necessarily available on the same machine as your web server.

HTML version of Basic Foils prepared 18 May 97

Foil 5 The Simplest Java Application: Hello, World!

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables and functions called methods.

HTML version of Basic Foils prepared 18 May 97

Foil 6 Java Applets

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Java applets are classes written in Java which are intended not to run as stand-alone programs (as applications do) but as subprograms of a browser which is already managing a window.
Applets should NOT have main method but rather init, start, paint etc. for displaying on the browser window
The applet should be run through javac compiler getting a .class file as before: javac MyApplet.java
Also create an HTML file (say MyApplet.html) with an applet tag.
Two ways to run an applet:
  • If you have JDK on one's machine, one can run the applet with appletviewer
    • appletviewer MyApplet.html
  • Alternatively, run Netscape 2.0/3.0 or Internet Explorer essentially anywhere, point the browser at MyApplet.html, and applet is downloaded from the webserver and run by Java interpreter built into the browser. This requires that the .html and .class files are located in the document space of the web server.
This way we can compile on places with JDK installed but run almost anywhere!

HTML version of Basic Foils prepared 18 May 97

Foil 7 Applet Tag: Calling Applets from HTML - I

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Given the following HTML
  • <APPLET CODE="StockGraph.class"
    • CODEBASE="http://www.javasoft.com/applets/"
    • WIDTH=200 HEIGHT=200>
  • </APPLET>
Runs the "StockGraph.class" executable as an applet.
WIDTH and HEIGHT are attributes that are passed along to the applet.
If the optional CODEBASE attribute is provided, then load the executable image from the directory specified by CODEBASE.
  • Without the CODEBASE attribute, will look for StockGraph.class in the local server's hierarchy (relative to where the HTML was loaded)
  • With the CODEBASE attribute will look for StockGraph.class on the given http hierarchy.
Tag and attribute names are case insensitive.

HTML version of Basic Foils prepared 18 May 97

Foil 8 Applet Tag: Calling Applets from HTML - II

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
<APPLET CODE="StockGraph.class" WIDTH=200 HEIGHT=200
  • ALT="-- StockGraph Not Supported --"
  • NAME=SUNW ALIGN=top
  • VSPACE=5 HSPACE=5>
Put a bunch of text here to be displayed by browsers such as Netscape 2.0 on Windows 3.1 that do not support Java
</APPLET>
ALT specifies text to displayed if the browser understands the applet tag, but if unable to run applets.
NAME specifies the name of this instance of the applet; This will make it possible for applets on the same page to find and communicate with each other.
ALIGN specifies the alignment of the applet. The possible values are the same as those available in the IMG tag (top, middle, bottom, texttop, absmiddle, baseline, absbottom, left, right).
  • Align=top which aligns top of applet with top of tallest item in the line
  • Align=texttop which aligns top of applet with top of the tallest text in the line
VSPACE and HSPACE specifies the vertical and horizontal spacing in pixels, around the applet space.

HTML version of Basic Foils prepared 18 May 97

Foil 9 <param> Tags and Applets

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The applet tag can be followed by parameters:
  • <applet . . . >
  • <param name=attributename1 value="attributevalue1" >
  • .......
  • <param name=attributenameN value="attributevalueN" >
  • </applet>
The Java program accesses this information by
  • String attribute;
  • attribute = getParameter("attributename1");
  • if( attribute == null )
  • attribute = yourdefaultvalue;
    • // null is Java way of saying unset
Typically this processing would be in init() method of Applet

HTML version of Basic Foils prepared 18 May 97

Foil 10 The Simplest Java Applet: Hello, World!

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Java applets are part of the class hierarchy that can call methods to display on a screen (within the browser window). This example defines the public method paint in this class and calls a method drawString defined in the class Graphics.

HTML version of Basic Foils prepared 18 May 97

Foil 11 Displaying your applet from a Web page.

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
You should name the file with your applet name, HelloWorldApplet.java, run the compiler (javac), getting a bytecode file HelloWorldApplet.class, which you put in a web directory.

HTML version of Basic Foils prepared 18 May 97

Foil 12 Java Language Basics

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 13 Java Language -- Lexical Issues I

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Lexical structure inherits a lot from C/C++. There are however some notable differences which are listed below.
Java characters are based on 16--bit wide Unicode Worldwide Character Encoding rather than the usual 8--bit wide ASCII.
This allows full support of all alphabets and hence all languages
Three types of comments are supported:
  • // ignore all till the end of this line
  • /* ignore all between starts */
  • /** an insert into an automatically generated software documentation */
for /** */ one inserts HTML documentation with some simple macros such as @see (to designate see also) BEFORE the method or class being documented

HTML version of Basic Foils prepared 18 May 97

Foil 14 Java Language -- Lexical Issues II

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Java reserves the following keywords:
  • abstract boolean break byte case catch
  • char class const continue default do double
  • else extends final finally float for
  • goto if implements import instanceof int
  • interface long native new
  • package private protected public return
  • short static super switch synchronized this
  • throw throws transient try void volatile while
Note goto is not allowed in Java but its still reserved!
null true and false are literals with special meaning but not keywords

HTML version of Basic Foils prepared 18 May 97

Foil 15 Java Language -- Program Structure

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Source code of a Java program consists of one or more compilation units, implemented as files with .java extension.
Each compilation unit can contain:
  • a package statement
  • import statements
  • class declarations
  • interface declarations
Java compiler (called javac) reads java source and produces a set of binary bytecode files with .class extensions, one for each class declared in the source file. For example, if Foo.java implements Foo and Fred classes, then "javac Foo.java" will generate Foo.class and Fred.class files.
Suppose that Foo implements an applet and Fred is an auxiliary class used by Foo. If Netscape/Internet Explorer encounters a tag <APPLET code="Foo.class">, it will download Foo.class and Fred.class files and it will start interpreting bytecodes in Foo.class.

HTML version of Basic Foils prepared 18 May 97

Foil 16 Java Language -- Variable/Expression Types

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Each Java variable or expression has a definite type, given by a declaration such as"int i;". There are three "types" of types!
  • There are Primitive or Simple types such as integers or booleans which are built-in.
  • New composite types (objects) can be constructed in terms of classes and interfaces. The type of an object is its class or interface
  • Arrays we will see are a sort of "almost" object!
First we discuss the Primitive Types
  • There are 4 integer types: byte, short, int, long of size 8, 16, 32 and 64 bits, respectively.
  • float is 32 bits, double is 64 bits. Floating point arithmetic and data formats are defined by IEEE754 standard.
  • characters are given by 16bit Unicode charset and represented as short integers.
One can use casts for conversion such as longint = (long) i; // which can be explicit as here and sometimes implied (see later)
Note booleans are either TRUE or FALSE -- they are not 0, 1 ,-1 ...

HTML version of Basic Foils prepared 18 May 97

Foil 17 Java Language -- Types: Array

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Arrays are "true" or "first class" objects in Java and no pointer arithmetic is supported.
An Array is declared as:
  • int vec[];
alternative syntax: int[] vec;
and created by:
  • vec = new int[128];
or concisely:
  • int vec[] = new int[128];
Arrays of arbitrary objects can be constructed,
  • e.g. Thread myThreadList[] = new Thread[1024];
  • The only difference is that in the case of primitive types, the array elements are actually allocated. In the case of arbitrary objects, an array of object references is created; before you use array elements, you must call the constructor of that type for each element.

HTML version of Basic Foils prepared 18 May 97

Foil 18 Java Language -- More on Arrays

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
An array of length 128 is subscripted by integers from 0 to 127.
Subscripts are range checked in runtime and so vec[-1] and vec[128] will generate exceptions.
Array length can be extracted via the length instance variable, e.g.
  • int len = vec.length will assign len = 128.
Arrays can have dynamic sizing (a fixed size determined at runtime)
  • int sizeofarray = 67;
  • int vec[] = new int[sizeofarray];
Multidimensional arrays are arrays of arrays
  • char icon[][] = new char[16][16]
  • These arrays can be "ragged":
    • int graph[][] = new int[2][];
    • graph[0][] = new int[4];
    • graph[1][] = new int[7];

HTML version of Basic Foils prepared 18 May 97

Foil 19 Java Language -- Expressions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Java's expressions are very similar to C and the following are valid:
2+3
(2+3)*i
i++ /* equivalent to i = i +1 */
(i > 0 ) && (j>0) /* Boolean */
i <<1 /* Left shift by 1 binary digit */
(i>0) ? true:false /* conditional expression */
i >>> 2 /* Signed right shift by 2 binary digits */
"fred" + "jim" is "fredjim"
/* + Equivalent to . in Perl */
(a instanceof B) /* True iff object a is of class B */

HTML version of Basic Foils prepared 18 May 97

Foil 20 Java Language -- Control Flow I

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
if(some boolean expression) { .. }
else if(another boolean) { .. }
else { ... }
while(any boolean) { /* Do Stuff */ }
do { /* What to do */ } while(another boolean);
for(expression1; booleanexpression ; expression2) { ...}
naturally starts with expression1, applies expression2 at end of each loop, and continues as long as booleanexpression true
switch (expression) { /* Just as in C */
case Constant1: /* Do following if expression=Constant1 */
/* Bunch of Stuff */
break;
case Constant2: /* Do following if expression=Constant2 */
/* Bunch of Stuff */
break; /* ....... */
default:
/* Bunch of Stuff */
break;
}

HTML version of Basic Foils prepared 18 May 97

Foil 21 Java Language -- Control Flow II -- continue

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One can break out of an iteration of a (nested) for loops in fashion offered by Perl but with a different syntax
outer: // label
for( int j=0; j<10; j++) { /* Note j only defined in for loop */
/* select course j */
  • for( int i=0; i<15; i++) {
  • if(studentgrade[j][i] == 4.0) {
    • /* Celebrate */
    • continue outer; // go to next iteration of outer loop
  • }
  • }
/* Continue jumps to here to next iteration of loop labelled with outer */
}

HTML version of Basic Foils prepared 18 May 97

Foil 22 Java Language -- Control Flow III -- break and for loop

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One can break out of (nested) for loops in fashion offered by Perl with different syntax
outer: // label
for( int j=0; j<10; j++) { /* Note j only defined in for loop */
/* select course j */
  • for( int i=0; i<15; i++) {
  • if(studentgrade[j][i] == 4.0) {
    • /* Celebrate */
    • break outer; // go to end of outer loop
  • }
  • }
}
/* break jumps to here out of loop labelled by outer */

HTML version of Basic Foils prepared 18 May 97

Foil 23 Java Language -- Control Flow IV -- break and switch

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
loopstart: // label
for( int j=0; j <10; j++) {
switch(j) {
case 3:
break;
default:
if( studentgrade[j] == 2.5)
  • break loopstart; /* go to end of loop */
/* do some stuff */
break;
}
}
/* break loopstart goes to here out of loopstart loop */

HTML version of Basic Foils prepared 18 May 97

Foil 24 Java Language -- Control Flow V -- continue and switch

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
loopstart: // label of following for loop
for( int j=0; j <10; j++) {
switch(j) {
case 3:
break;
default:
if( studentgrade[j] == 2.5)
  • continue loopstart; /* go to next iteration of loop */
/* do some stuff */
break;
} // End Switch Block
/* continue loopstart goes to here for next iteration of loopstart loop */
} // End loopstart for loop

HTML version of Basic Foils prepared 18 May 97

Foil 25 Basic Graphics Java Applets
to illustrate language
(more later!)

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 26 The Graphics class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
An object of the graphics class represent a rectangular drawing grid, with a coordinate system in pixels.
When you draw objects, there is a current "state" consisting of a font and a color.

HTML version of Basic Foils prepared 18 May 97

Foil 27 Using Graphics properties

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Fonts and colors are objects (sometimes called instances) of the font and color class in the awt package.

HTML version of Basic Foils prepared 18 May 97

Foil 28 Drawing simple shapes

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Methods are available for drawing lines, rectangles, rounded rectangles, ovals, arcs, and polygons. This example draws a rectangle filled with green with a black border, and a circle filled with magenta.

HTML version of Basic Foils prepared 18 May 97

Foil 29 Passing parameters to an applet: the HTML

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Within the applet tags, any number of param tags may occur. Attributes can control the alignment of the applet window on the html page.

HTML version of Basic Foils prepared 18 May 97

Foil 30 Passing parameters to an applet: the applet

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
A standard method, init(), is executed when your applet is loaded; it can call the method getParameter.

HTML version of Basic Foils prepared 18 May 97

Foil 31 Centering a string in the applet window.

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The method applet.size() returns a dimension object, which has width and height variables. FontMetrics has variables for various attributes of the font, including width and height.

HTML version of Basic Foils prepared 18 May 97

Foil 32 Applet Flow of Control

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Each applet's flow of control goes through a sequence of standard methods:
  • public void init() { . . . }
  • executed once when applet is downloaded to the browser.
  • public void start(){ . . . }
  • executed each time the browser window is entered.
  • public void paint(){ . . . }
  • executed whenever you want to draw in the window or when browser needs to refresh window.
  • public void stop(){ . . . }
  • executed each time the browser window in exited.
  • public void destroy(){ . . . }
  • executed just before the applet exits.
There are also standard methods repaint (), which calls update(), which blanks out the old drawing and calls paint.

HTML version of Basic Foils prepared 18 May 97

Foil 33 Random color boxes example

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This example fills the applet's drawing area with square boxes, each of which has a randomly chosen color.

HTML version of Basic Foils prepared 18 May 97

Foil 34 Arrays of random colors

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This demonstrates 2-dimensional arrays by first filling arrays with random colors, and then drawing, uses a fixed size drawing window for simplicity.

HTML version of Basic Foils prepared 18 May 97

Foil 35 The Java Object Model: Classes, Instances and Methods

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 36 The Java Object Model Overview

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Programs are composed of a set of modules called classes. Each class is a template
specifying a set of behaviors on
the data of the class.
Each class has class variables
(sometimes called instance vars)
to hold the data and methods (called
functions or procedures in other
languages) define the behaviors.
Each object in a program is created
as an instance of a class. Each class
instance has its own copy of the class
variables.
Classes can be used for data encapsulation, hiding the details of the data representation from the user of the class.

HTML version of Basic Foils prepared 18 May 97

Foil 37 API of a Class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Each class has an API (Application Programming Interface) consisting of all the variables and methods that other programmers (i.e. in other classes) are allowed to use. These are designated by the "public" keyword.
Example showing part of the Java Date class:
  • public class Date
  • { // Constructor methods to create instances of class
    • public Date();
    • public Date(int year, int month, int day);
  • // Accessor and Mutator methods to access and change data
    • public int getHours();
    • public int getYear();
    • . . .
    • public void setHours();
    • public void setYear();
    • . . .
  • // Other public methods
    • public boolean after(Date when);
    • . . . }
The on-line Java Hierarchy and Index shows the API's of all Java classes.

HTML version of Basic Foils prepared 18 May 97

Foil 38 Using a Class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This declares object today to have type class
    • Date today
Date() is Constructor of Date class which constructs an instance of Date class and sets default value to be the current date
    • new Date()
An example application using a method of the Date class:
  • import java.util.Date;
  • class DateApplication
  • { public static void main ()
  • { Date today = new Date();
  • Date medieval = new Date(1400, 12, 25);
  • if (today.after (medieval))
    • System.out.println( "Today is not medieval!");
    • }}

HTML version of Basic Foils prepared 18 May 97

Foil 39 A Computational Class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
A class (such as a "main routine") may also be implemented to have just one computational instance.
This application reads from standard input and counts number of characters which are then printed
class Count {
public static void main (String args[])
throws java.io.IOException
{ int count = 0;
  • while (System.in.read() != -1)
  • count++;
  • System.out.println("Input has " + count + " chars.");
}}

HTML version of Basic Foils prepared 18 May 97

Foil 40 Class Definitions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Class declaration in Java shares common aspects with C++ but there are also some syntactic and semantic differences.
Only single inheritance is supported but aspects of multiple inheritance can be achieved in terms of the interface construct. Interface is similar to an abstract class with all methods being abstract and with all variables being static (independent of instance). Unlike classes, interfaces can be multiply-inherited.
ClassModifiers class className [extends superClass] [implements interfaces] {
e.g. public class Test extends Applet implements Runnable {
defines an applet that can use threads which have methods defined by Runnable interface

HTML version of Basic Foils prepared 18 May 97

Foil 41 Constructors of Objects

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Methods of identical name to class are special -- they are constructors
mPoint() { /* Default Constructor */
  • x=0;
  • y=0;
}
We can overload method to define different versions with different numbers and types of arguments
mPoint(int _x, int _y) { /* Constructor to set Specific Initial values */
  • x = _x;
  • y = _y;
}
mPoint can have other methods to set its instance variables such as:
public void setDelta(int _dx, int _dy) {
  • dx = _dx;
  • dy = _dy;
}

HTML version of Basic Foils prepared 18 May 97

Foil 42 Java Language -- Types of Classes - I

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Possible ClassModifiers are:
abstract -- Contains abstract methods without implementation -- typically such abstract classes have several subclasses that define implementation of methods
public -- May be used by code outside the class package and (unix) file must be called ClassName.java where ClassName is unique public class in file
private -- this class can only be used within current file -- i.e current class
friendly(i.e. empty ClassModifier) -- class can be used only within current package
protected -- Only accessible to subclasses

HTML version of Basic Foils prepared 18 May 97

Foil 43 Java Language -- Types of Classes - II

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
threadsafe: Instance or static variables will never change asynchronously and so can use compiler optimizations such as assigning to registers. Next modifier -- final -- is also valuable to compilers
final -- Cannot have a subclass for classes
  • cannot be overridden for methods
  • final variables have a constant value e.g.
    • final int ageatdeath = 101;
transient -- specifies that objects are not persistent
Note most of these modifiers can be used either for a class or an object -- a particular instance of a class
  • abstract only makes sense for a class and transient is perhaps more useful on an object basis

HTML version of Basic Foils prepared 18 May 97

Foil 44 Java Language -- Types of Methods

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
MethodModifier ReturnType Name(argType1 arg1, ......)
Returntypes are either simple types (int, byte etc.), arrays or class names
Possible MethodModifiers are:
  • public -- This method is accessible by all methods inside and outside class
  • protected -- This method is only accessible by a subclass
  • private -- This method is only accessible to other methods in this class
  • friendly(i.e. empty) -- This method is accessible by methods in classes that are in same package
  • final -- a method that cannot be overriden
  • static -- This method is shared by ALL instances of this class and can be invoked with <Class>.method syntax as well as <Instance>.method
  • synchronized -- This method locks object on entry and unlocks it on exit. If the object is already locked, the method waits until the lock is released before executing -- can be used on methods or statement blocks
  • native -- to declare methods implemented in a platform -- dependent language, e.g. C.

HTML version of Basic Foils prepared 18 May 97

Foil 45 The Java Object Model: Inheritance and the Class Hierarchy

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 46 Relationships between Classes

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
use
  • A uses B: A calls a method (sends a message to) an object of class B or creates, receives, or returns an object of class B.
containment
  • A has a B: special case of use - an object of class A contains an object of B
inheritance
  • B is a A: specialization - B extends A (is a subclass of A) if B has all the variables and methods of A (and more).
  • In the class definition of B, the child class, there is no need to repeat declarations of variables and methods of A, they are assumed to be there. The definition of B has the additional variables and methods of B.

HTML version of Basic Foils prepared 18 May 97

Foil 47 Use of Methods Defined in Parent

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Call to method in Object2 (message) from object1 is passed up the class hierarchy until a definition is found

HTML version of Basic Foils prepared 18 May 97

Foil 48 Use of Methods Defined in Parent but overridden in child class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Call to method in Object2 (message) from object1 is passed up the class hierarchy until a definition is found

HTML version of Basic Foils prepared 18 May 97

Foil 49 Comments on Casting

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Casting (type conversion) is supported between types and class types. Syntax:
  • (classname)reference
Two forms of casting are possible: widening and narrowing
Widening, where the subclass is used as an instance of the superclass, is performed implicitly
Narrowing, where the superclass is used as an instance of the subclass, must be performed explicitly
Given Parent: Dot -> DrawableDot (Child):
  • Widening: An instance of DrawableDot is used as an instance of Dot
  • Narrowing: An instance of Dot is used as an instance of DrawableDot
Casting between sibling classes is a compile-time error

HTML version of Basic Foils prepared 18 May 97

Foil 50 Array - A Pseudo Class!

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Not in any package
One final instance variable: length
For each primitive type (and all classes), there's an implicit Array subclass
Cannot be extended (subclassed)
Superclass is Object
Inherits methods from Object
(new int[5]).getClass().getSuperclass()
will return Java.lang.Object

HTML version of Basic Foils prepared 18 May 97

Foil 51 By value and By reference

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Many languages are confusing as they differ in often unstated distinction between the value and "handle" -- Java is no exception! (reference,address,pointer) of an entity
Consider assignment: a = b; // sets value of a to value of b
If a and b are primitive types, then they hold "actual literals" and so if b=66, then a is set to 66
  • In this case if you change b, then a is left unchanged
However if a or b is an object, b is in fact a reference and so one sets a to refer to same object as b (i.e. same "location" in memory)
  • if you change b in some way, then a will be changed accordingly
  • Note null is value of an object which has not been assigned (constructed) and so does not point anywhere
Arguments to Methods are always passed by value BUT if an object is an argument, then that value is ALWAYS a reference and so in practice
  • Primitive types are passed by value
  • Objects are passed by reference
Arrays reflect properties of what they are arrays of!

HTML version of Basic Foils prepared 18 May 97

Foil 52 Comments on Overloading and Overriding in Classes

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Overriding Methods (where child class provides method with same signature as method in parent)
  • To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list.
  • When the method is invoked on an instance of the subclass, the new method is called rather than the original method.
  • The overridden method can be invoked using the super variable.
  • Super can be used to refer to instance variables in the superclass as well.
Overloading (where a class can provide a set of methods all with the same name, but with different signatures): The signature is defined (as in Arnold-Gosling book) by
  • Lowest conversion cost of parameter list, based on type and number of parameters. Return type and declaration order not important.
  • Java will declare an error if method is invoked where there is not one with a unique signature which matches call after removing less specific methods (use of objects in hierarchy can cause lots of confusing matches!)

HTML version of Basic Foils prepared 18 May 97

Foil 53 Object-Oriented
Programming Examples:
Basic Class Operations

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 54 The Class Hierarchy of this example

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 55 Object-Oriented Programming in JAVA - defining a class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
We define a parent class for movable point objects. Each instance of this class is represented by an x,y location, by a dx,dy offset for the object to move, and a color for the object. This example also illustrates data encapsulation, where users of the class must use methods to get data of the class.

HTML version of Basic Foils prepared 18 May 97

Foil 56 More methods in the class definition

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
We include methods to change the default values for the offsets and color, a method to move the object, and one to paint a point.

HTML version of Basic Foils prepared 18 May 97

Foil 57 Defining a child class

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
We define a class for movable rectangle objects. It uses x,y from the parent class mPoint for the location of the rectangle, but adds h,w to specify height and width of the rectangle.

HTML version of Basic Foils prepared 18 May 97

Foil 58 Using parent and child classes

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
This applet creates 2 mRectangles and loops to move them.

HTML version of Basic Foils prepared 18 May 97

Foil 59 Parent and child example, continued

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The repaint method calls update, which is overridden here to move the objects, and paint, which is overridden here to redraw whole graphics area.

HTML version of Basic Foils prepared 18 May 97

Foil 60 More child classes

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Once we have structured the concepts of our data into classes, it is easy to add new shapes of movable objects as child classes of mRectangle. These classes only have to redefine paint to draw a differently shaped object.

HTML version of Basic Foils prepared 18 May 97

Foil 61 Abstract Methods
and Classes
Interfaces
(classes without implementation)

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 62 Abstract Methods and Classes

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
An abstract method has no body - it is provided in a class to define the signature of the method for program structuring purposes. It must be defined in some subclass of the class in which it is declared.
  • Constructors, static methods, private methods cannot be abstract
  • A method that overrides a superclass method cannot be abstract
Classes that contain abstract methods and classes that inherit abstract methods without overriding them are considered abstract classes
  • It is compile-time error to instantiate an abstract class or attempt to call an abstract method directly.

HTML version of Basic Foils prepared 18 May 97

Foil 63 Java Language -- Interfaces - Overview

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
An interface specifies a collection of methods (behaviors) without implementing their bodies (akin to giving the API).
  • public interface Storable {
    • public abstract void store(Stream s);
    • public abstract void retrieve(Stream s);
    • }
Any other class which implements the interface is guaranteeing that it will have the set of behaviors, and will give concrete bodies to the methods of the interface.
Interfaces solve some of the same problems as multiple inheritance, without as much overhead at runtime.
  • There is a small performance penalty because interfaces involve dynamic method binding.
Interfaces can be implemented by classes on unrelated inheritance trees, making it unnecessary to add methods to common superclass.

HTML version of Basic Foils prepared 18 May 97

Foil 64 Cars as an Examples of Interfaces/ Multiple Inheritance

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
We could imagine a Ford Mustang as inheriting from two major sources
Firstly a set of manufacturing companies -- make these interfaces as "qualitative"
Secondly from a set of Vehicle types which we will make real classes as there are non trivial methods involved in constructing cars

HTML version of Basic Foils prepared 18 May 97

Foil 65 Picture of Interfaces and Classes for Cars and their Manufacture

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Cars MyFordMustang = new Cars(Lots of Options)
is a particluar instance of class Cars

HTML version of Basic Foils prepared 18 May 97

Foil 66 Java Language -- Interface Example -- Implementing Storable

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
A class may implement an interface, in which case it provides the body for the methods specified in the interface.
interface storable has store and retrieve methods
  • public class Picture implements Storable {
    • public void store(Stream s) {
    • // JPEG compress image before storing
    • }
    • public void retrieve(Stream s) {
    • // JPEG decompress image before retrieving
    • }
  • }
  • public class StudentRecord implements Storable {
    • . . .
  • }

HTML version of Basic Foils prepared 18 May 97

Foil 67 Interfaces can be used as Classes in type specification

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Interfaces behave exactly as classes when used as a type.
The normal type declaration syntax "interfaceName variableName" declares a variable or parameter to be an instance of some class that implements interfaceName.
  • public class StudentBody {
    • Stream s;
    • Picture id_photo; // of interface storable
    • StudentRecord id_card; // of interface storable
    • . . .
    • public void register() {
    • save(id_photo);
    • save(id_card);
    • }
    • public void save(Storable o) {
    • o.store(s);
    • }
  • }

HTML version of Basic Foils prepared 18 May 97

Foil 68 Further Features of Interfaces

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Interfaces are either public or have the default friendly access (public for the package and private elsewhere)
Methods in an interface are always abstract and have the same access as the interface. No other modifiers may be applied
Variables in an interface are public, static, and final. They must be initialized.
When a class implements an interface:
  • it implements all the methods described in the interface
  • or it is an abstract class, which leaves the implementation of some or all of the interface methods to its subclasses
Interfaces can incorporate one or more other interfaces, using the extends keyword:
  • public interface DoesItAll extends Storable, Paintable {
    • public abstract void doesSomethingElse();
  • }
A class can implement more than one interface:
  • public class Picture implements Storable, Paintable {
    • public void store(Stream s) {...}
    • public void retrieve(Stream s) {...}
    • public void refresh() {...}
  • }

HTML version of Basic Foils prepared 18 May 97

Foil 69 More on Interfaces -- Why use them

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Note that Interfaces often play a software engineering as opposed to required functional role
Note that Interfaces are not significantly used in current Java release where perhaps there are 15 times as many class definitions as interface definitions
Two examples are Runnable and Cloneable both of which extend Object class -- note interfaces like classes can extend existing classes.
The Runnable Interface has one method run() which must always be overwritten and is used to indicate that class with this interface can use threads without being a subclass of Thread. Applets must use Runnable if they need explicit threads because they explicitly are a subclass of panel and as no multiple inheritance, one cannot be a subclass of two classes

HTML version of Basic Foils prepared 18 May 97

Foil 70 Packages in Java

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 71 Overview of Packages and Directory Structure

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One file can contain several related classes, but only one of them can be public. If the public class is called wheat.java, then the file must be called wheat.
A set of classes in different files can be grouped together in a package. Each of the files must be in the same directory and contain the command
  • package mill;
The name of the directory must also be mill.

HTML version of Basic Foils prepared 18 May 97

Foil 72 Using Java packages

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
One conveniently uses files in a package by inserting
  • import mill.*
at the beginning of a file that needs classes from the mill package
  • Then classes in the mill package can be refered to by just using their Classname
  • Without the import command, one must explicitly say mill.Classname
Packages can be grouped hierarchically, with the corresponding directory tree. For example, the mill package could be a subpackage of agriculture. Then a class is referred to as agriculture.mill.Classname.
Except for classes provided with the Java language, all of which have the form java.X, a class that is imported or used must either be in the current directory or be accessible to the compiler through the CLASSPATH environment variable.

HTML version of Basic Foils prepared 18 May 97

Foil 73 Java 1.0 System Packages

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
java.lang Contains essential Java classes and is by default imported into every Java file and so import java.lang.* is unnecessary. Thread, Math, Object and Type Wrappers are here
java.io contains classes to do I/O. This is not necessary (or even allowed!) for applets which can't do much I/O in Netscape!
java.util contains various utility classes that didn't make it to java.lang. Date is here as are hashtables
java.net contains classes to do network applications. This will be important for any distributed applications
java.applet has the classes needed to support applets
java.awt has the classes to support windowing -- The Abstract Windows Toolkit
java.awt.image has image processing classes
java.awt.peer is a secret set of classes with platform dependent details

HTML version of Basic Foils prepared 18 May 97

Foil 74 More on the Java Language: Exceptions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index

HTML version of Basic Foils prepared 18 May 97

Foil 75 Java Language -- Handling Runtime Errors Using Exceptions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
The language itself supports concept of an exception
Java supports a hierarchical model of exceptions which allow and indeed require user to supply suitable handlers for any exception that can occur in a Java program
Note exceptions that can occur in a method must either be caught (i.e. handled inside method) or thrown (i.e. returned to callee)
Thrown exceptions are like returned arguments and are for instance part of interface to a method
Exceptions are all (at some level in hierarchy) subclasses of Throwable class

HTML version of Basic Foils prepared 18 May 97

Foil 76 User Created Exceptions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
Exception class has two constructors, one of which allows a message to be included in each instance.
The user can either throw an exception of type Exception with a unique message, or create own subclass of Exception:
public static void MyMethod() throws MyException
  • { . . .
  • throw new MyException;
  • . . . }
class MyException extends Exception
  • { public MyException ()
  • { super ("This is my exception message."); }
  • }
Methods which call "MyMethod" should use a try and catch block which catches an exception e of type MyException. Methods e.getMessage and e.printStackTrace can be used on Exceptions.

HTML version of Basic Foils prepared 18 May 97

Foil 77 Basic Structure of Exception Handling in Nested Calls

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
method1 {
  • try {
  • call method2;
  • } catch (Exception3 e) {
  • doErrorProcessing(e);
  • }
}
method2 throws Exception3 {
  • call method3; // method2 just passes exception through
}
method3 throws Exception3 {
  • call dividebyzeroorreadfileorsomething; // create exception
}

HTML version of Basic Foils prepared 18 May 97

Foil 78 Examples of Exception Hierarchy

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
As Examples of hierarchy:
catch(FileNotFoundException e) { .. } would catch particular exception whereas
catch(IOException e) { .. } would catch all IOexceptions

HTML version of Basic Foils prepared 18 May 97

Foil 79 Example of Handling Exceptions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
File file; /* defines file to be object of class File */
try{
  • file = new File("filenameyouwant");
  • . . . . .
  • file.write("stuff put out");
} catch (IOException e) {
  • /* This catches ALL I/O errors including
    • read and write stuff */
  • /* Handle Exception somehow */
return;
}
/* but the optional finally clause will be executed
    • whether or not code terminates normally */
finally {
  • file.close();
}

HTML version of Basic Foils prepared 18 May 97

Foil 80 Classes of Exceptions

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
There are two subclasses of Throwable
  • Error such as OutOfMemoryError which do NOT have to be caught as they are serious but unpredictable and could typically occur anywhere!
  • Exception which we have discussed
Exception has a subclass RuntimeException that need NOT be caught
Typical RuntimeException subclasses are
ArithmeticException, ClassCastException, IndexOutofBoundException

HTML version of Basic Foils prepared 18 May 97

Foil 81 Exceptions in Applets

From Java Tutorial - Spring 1997 Part II: Java Language and Object-Oriented Concepts Peking Tutorial, Web Certificate -- Spring-Summer 1997. *
Full HTML Index
When writing a Java applet, your code is overriding one of the standard applet methods, and you are not allowed to throw any exceptions that it would not. So, in general, you must handle exceptions.
What to do: The standard trick of writing a message to System.out works fine for debugging when running with the applet viewer. It also works fine with the Netscape browser for errors that you don't really expect to happen in working code (like your code had a null pointer) because Netscape provides a "Java console" under the Options menu that displays all messages.
However, for errors that you really want to bring to the attention of the user, such as they typed in their URL incorrectly and the network methods returned a "malformedURLException", you can put up a pop-up window in the style of professional GUI programs.
Note that you don't have to micromanage exceptions - you don't have to put a "try-catch" construct around every statement that can throw an exception. You can put the "try-catch" around the entire code with the same catch action or even put different catches for different actions of the errors.

© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Thu Jan 8 1998