Given by Nancy J. McCracken,Geoffrey C. Fox at CEWES Tutorial on July 22-25 1997. Foils prepared 19 July 97
Outside Index
Summary of Material
In Part 1 of the Tutorial We Covered:
|
This Part(2) of Tutorial Covers |
Java Programming Language
|
Object Oriented and Class Structure
|
Exceptions |
And in the Remaining Parts of the Java Tutorial We Cover:
|
Outside Index Summary of Material
Instructors: Geoffrey Fox , |
Nancy McCracken |
Syracuse University |
111 College Place |
Syracuse |
New York 13244-4100 |
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:
|
for /** */ one inserts HTML documentation with some simple macros such as @see (to designate see also) BEFORE the method or class being documented |
Java reserves the following keywords:
|
Note goto is not allowed in Java but its still reserved! |
null true and false are literals with special meaning but not keywords |
Source code of a Java program consists of one or more compilation units, implemented as files with .java extension. |
Each compilation unit can contain:
|
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. |
Each Java variable or expression has a definite type, given by a declaration such as"int i;". There are three "types" of types!
|
First we discuss the Primitive Types
|
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 ... |
Arrays are "true" or "first class" objects in Java and no pointer arithmetic is supported. |
An Array is declared as:
|
alternative syntax: int[] vec; |
and created by:
|
or concisely:
|
Arrays of arbitrary objects can be constructed,
|
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.
|
Arrays can have dynamic sizing (a fixed size determined at runtime)
|
Multidimensional arrays are arrays of arrays
|
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 */ |
"fred" + "jim" is "fredjim" |
/* + Equivalent to . in Perl */ |
(a instanceof B) /* True iff object a is of class B */ |
if(some boolean expression) { .. } |
else { ... }
|
while(any boolean) { /* Do Stuff */ } |
do { /* What to do */ } while (another boolean); |
for(expression1; booleanexpression ; expression2) { ...}
|
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; |
} |
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 */
|
/* Continue jumps to here to next iteration of loop labelled with outer */ |
} |
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 */
|
} |
/* break jumps to here out of loop labelled by outer */ |
loopstart: // label |
for( int j=0; j <10; j++) { |
switch(j) { |
case 3: |
break; |
default: |
if( studentgrade[j] == 2.5)
|
/* do some stuff */ |
break; |
} |
} |
/* break loopstart goes to here out of loopstart loop */ |
loopstart: // label of following for loop |
for( int j=0; j <10; j++) { |
switch(j) { |
case 3: |
break; |
default: |
if( studentgrade[j] == 2.5)
|
/* do some stuff */ |
break; |
} // End Switch Block |
/* continue loopstart goes to here for next iteration of loopstart loop */ |
} // End loopstart for loop |
Subprograms in Java are called methods. The definition format is
|
The parameter list contains the types and names of all the parameters. |
The declarations and statements are called the body of the method. Parameter names and variables declared in the body are local to it. |
Control returns from the methods either when the body is finished execution or a return statement is encountered. Return statements may also return a result. |
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) to 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 (by marking variables as private). |
The class definition consists of
|
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:
|
The on-line Java Hierarchy and Index shows the API's of all Java classes. |
This declares object today to have type class
|
Date() is Constructor of Date class which constructs an instance of Date class and sets default value to be the current date
|
An example application using a method of the Date class:
|
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;
|
}} |
Class declaration in Java shares common aspects with C++ but there are also some syntactic and semantic differences. |
ClassModifiers class className [extends superClass] [implements interfaces] { <body of class>} |
e.g. public class Test extends Applet implements Runnable { . . . } |
defines an applet that can use threads which have methods defined by Runnable interface |
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. |
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 |
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
|
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
|
MethodModifier ReturnType Name(argType1 arg1, ......) |
Returntypes are either simple types (int, byte etc.), arrays or class names |
Possible MethodModifiers are:
|
use
|
containment
|
inheritance
|
Call to method in Object2 (message) from object1 is passed up the class hierarchy until a definition is found |
Call to method in Object2 (message) from object1 is passed up the class hierarchy until a definition is found |
Casting (type conversion) is supported between types and class types. Syntax:
|
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):
|
Casting between sibling classes is a compile-time error |
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 |
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
|
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)
|
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
|
Arrays reflect properties of what they are arrays of! |
Overriding Methods (where child class provides method with same signature as method in parent)
|
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
|
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.
|
Classes that contain abstract methods and classes that inherit abstract methods without overriding them are considered abstract classes
|
An interface specifies a collection of methods (behaviors) without implementing their bodies (akin to giving the API).
|
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.
|
Interfaces can be implemented by classes on unrelated inheritance trees, making it unnecessary to add methods to common superclass. |
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 |
Cars MyFordMustang = new Cars(Lots of Options) |
is a particluar instance of class Cars |
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
|
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.
|
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:
|
Interfaces can incorporate one or more other interfaces, using the extends keyword:
|
A class can implement more than one interface:
|
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 |
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
|
The name of the directory must also be mill. |
One conveniently uses files in a package by inserting
|
at the beginning of a file that needs classes from the mill package
|
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. |
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 |
java.awt.datatransfer Classes and interfaces to transfer data from a Java program to the system clipboard (enabling drag-and-drop) |
java.beans Contains classes to write reusable software components |
java.lang.reflect Enables a program to discover the accessible variables and methods of a class at run-time |
java.rmi Remote Method Invocation |
java.security Enables a Java program to encrypt data and control the access privileges provided |
java.sql Java Database Connectivity (JDBC) enables Java programs to interact with a database using the SQL language |
java.text Classes that provide internationalization capabilities for numbers, dates, characters and strings |
java.util.zip Combines java .class files and other files into one compressed file called a Java archive (JAR) file. |
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 |
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
|
class MyException extends Exception
|
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. |
method1 {
|
} |
method2 throws Exception3 {
|
} |
method3 throws Exception3 {
|
} |
As Examples of hierarchy: |
catch(FileNotFoundException e) { .. } would catch particular exception whereas |
catch(IOException e) { .. } would catch all IOexceptions |
File file; /* defines file to be object of class File */ |
try{
|
} catch (IOException e) {
|
return; |
} |
/* but the optional finally clause will be executed
|
finally {
|
} |
There are two subclasses of Throwable
|
Exception has a subclass RuntimeException that need NOT be caught |
Typical RuntimeException subclasses are |
ArithmeticException, ClassCastException, IndexOutofBoundException |
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. |