CIS 121 January 31, 2000
Important Concepts
The finalize() method in an object cleans up the object prior to
being deallocated. It is used for instance to close a file that an
object opened.
A deprecated feature is one that is replaced as Java evolves.
Encapsulation is the concept of binding data together with the
code that acts on it.
Information Hiding is the concept of restricting access to information through
use of the access control keywords public, private, and protected.
A language is said to have support for Abstract Data Types (ADTs)
if it has support for encapsulation and information hiding.
The seven things which can be placed in a class definition. Recall
that we discussed the main five things that can be placed in a class
definition (class and instance variables, class and instance methods,
and constructors) in detail. The other two things which can be placed
in a class definition are static initializers and inner classes.
Static Initializers are similar to constructors in that they are
used to initialize class variables when the object or a class that uses
the object is loaded into memory. The method has only the keyword
static in front of it, it has no access control keyword, no return type,
no name, and no parameter list. Since simple class variables can be
initialized in their declaration, static initializers are used mainly
to initialize class variables that are arrays.
An inner class is a complete class definition inside of another
class definition that is only intended to be used within that class.
A package is a collection of similar Java classes. When two classes
are created in the same subdirectory, and the source code for the classes
does not contain an implicit package statement, then they are considered
to be in the same default class. When we use implicit package statements,
then the CLASSPATH environment variable can be used to help the compiler
and interpreter find classes.
There are four ways in which we contain obtain classes to solve
problems.
- See if the JDK has them.
- See if we can buy packages.
- See if we can find a class that is almost what we want and use
inheritance.
- Write our own.
CLASSPATH is an environment variable which contains a list of
directories seperated by semicolons. Suppose we have the following statement
in our source code.
import a.b;
The compiler (javac) will search through the directories listed in the
CLASSPATH until it finds a directory called a. Within that directory it
looks for a file called b.class. If it does not find the directory or the
file b.class does not exist inside the directory called a, then the
compiler reports an error.
Suppose we make the following call to the java interpreter (java).
java a.b
The intepreter will search through the directories listed in the CLASSPATH
until it finds a directory called a. Inside that directory it looks
for a file called b.class and executes it if it can. If it does not find
the directory called a or it does not find the class b.class inside the
directory called a, then it reports an error.
Garbage and Garbage collection - When an object is created, it is
allocated space on the memory heap, and we access this object through
an object reference. When we lose the ability to reference an object,
then that object is referred to as garbage. Garbage collection is the
detection and removal of garbage. The java interpreter keeps track of
when an object has become garbage and peforms garbage collection
automatically. First a call is made to the finalize() method of the
object if it exists, and then the space taken up by the object is reclaimed
for possible reuse. In other high-level languages, garbage collection
is not automatic.
Good Design Objectives - There are three desirable properties of
good programming design. The goal of object oriented programming is
to achieve a desired level of abstraction. That means we want to design
classes whose variables and methods capture the "essence" of the objects
of this type. These properties are referred to as the three C's, cohesion,
complete/competent classes, and loose coupling.
Cohesion means that we put together only the the things that are
necessary to the abstraction we are creating.
Complete/Competent Classes. One of the advantages of object oriented
programming is reuse of code. Therefore we should keep this is mind
when creating classes. For instance, if we create a class definition
with three methods, f1, f2, and f3, that would be used by any classes
that inherit this class, then we should implement all three even if
f1 and f2 would suit our needs for the current problem we are trying to
solving. Implementing f3 would indicate that we are thinking ahead to
future use of the class.
Loose coupling. When we have interaction among classes, e.g. one class
uses an instance of another class, then we don't want the first class to
rely on the internal implemenation of the other class. In some cases we
cannot avoid this, e.g. we change the parameter list to an instance method,
but we should try to minimize it as much as we can.
StreamTokenizer. Java offers us a class called StreamTokenizer.
This class is similar to the class StringTokenizer. However, there are
significant differences. StringTokenizer works on a string, offers a
method to count the number of tokens in the String, and then allows us
to fetch the tokens one at a time. StreamTokenizer works on an entire file
and does not offer the ability to count the number of tokens. Like
StringTokenizer, StreamTokenizer clears out white space in the file and
gives us back the tokens one at a time. The carriage return character
does not become a token. As we read the tokens in, we are given an integer
variable which represents the token type. The more important token types
are TT_EOF (Token Type - End of File), TT_NUMBER (Token Type - Number),
and TT_WORD (Token Type - Word).
TestStream.java