Full HTML for

Basic foilset Use of Java Language in Computational Science

Given by Nancy McCracken, Geoffrey C. Fox at DoD Modernization Users Group Conference Monterey on June 7 99. Foils prepared July 6 99
Outside Index Summary of Material


Overview of Java features and uses
Details of language features:
  • basic language
  • objects and classes
  • graphical user interface (AWT)
  • multi-threading
  • streams and I/O
  • networking
  • distributed objects (RMI)
  • servlets (CGI)
  • database connection (JDBC)
  • component programming (JavaBeans)
Java Grande Forum
  • performance
  • proposals for language extensions and libraries

Table of Contents for full HTML of Use of Java Language in Computational Science

Denote Foils where Image Critical
Denote Foils where Image has important information
Denote Foils where HTML is sufficient

1 Java for Scientific Computing
2 Abstract
3 Overview of Java features and uses in scientific computing
4 The language Java
5 Java is an important language in the world
6 Why use Java as the basis for HPCC/Scientific Computing Software?
7 The Computing Pyramid
8 Architecture of Java Applets
9 Architecture of Java Applications
10 Java Development Kit (JDK)
11 Java Applications
12 The Simplest Java Application: Hello,World!
13 Java Applets
14 Preparing an Applet
15 The Simplest Java Applet: Hello, World!
16 Displaying your applet from a Web page.
17 Multi-tier Architecture
18 Multi-tier architecture for Scientific Computing
19 Java in the three tiers of scientific computing
20 Resources for the Java Programming Language
21 Books on Java
22 Java Development Environments
23 Details of the Java Language
24 Java Language Basics
25 Types
26 Arrays
27 Example of basic language syntax
28 Object Model Overview
29 Class definition
30 An example of a class definition
31 Inheritance (the subclass relationship)
32 Access control
33 Classes are used as program modules
34 Packages
35 Using objects from Java packages
36 Exceptions
37 Data Structures
38 Abstract Windowing Toolkit (AWT)
39 Graphics Concepts
40 Graphics Methods
41 Basic components
42 Event Model - I
43 Event Model - II
44 Sketch of Event Handling applet
45 Layout Managers
46 Additional Capabilities of the AWT
47 Multi-threading
48 Introducing Threads
49 Animation Thread for Applet
50 Synchronization of threads
51 More about synchronization
52 Streams
53 I/O with Streams
54 Reading and Displaying data from a file
55 Network Connections with URL's
56 Sockets for Network Communication
57 Java Application Servers
58 Remote Method Interface (RMI)
59 RMI Architecture
60 Java Web Servers/Servlets
61 Java DataBase Connection (JDBC)
62 JDBC Architecture
63 Connecting to the Database
64 Database queries
65 JavaBeans
66 Features of JavaBeans
67 Component Programming with JavaBeans
68 Java Grande Forum
69 The Java Grande Forum
70 Activities of Java Grande Forum I
71 Activities of Java Grande Forum II
72 Proposal for class Complex
73 Proposal for Rectangular Array class
74 Proposal for Rectangular Array class

Outside Index Summary of Material



HTML version of Basic Foils prepared July 6 99

Foil 1 Java for Scientific Computing

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Geoffrey Fox and Nancy McCracken
Northeast Parallel Architectures Center
at Syracuse University
http://www.npac.syr.edu/projects/tutorials/Java
http://www.npac.syr.edu/projects/tutorials/JavaCSE

HTML version of Basic Foils prepared July 6 99

Foil 2 Abstract

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Overview of Java features and uses
Details of language features:
  • basic language
  • objects and classes
  • graphical user interface (AWT)
  • multi-threading
  • streams and I/O
  • networking
  • distributed objects (RMI)
  • servlets (CGI)
  • database connection (JDBC)
  • component programming (JavaBeans)
Java Grande Forum
  • performance
  • proposals for language extensions and libraries

HTML version of Basic Foils prepared July 6 99

Foil 3 Overview of Java features and uses in scientific computing

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index

HTML version of Basic Foils prepared July 6 99

Foil 4 The language Java

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
What is Java?
  • A simple, object oriented, distributed, interpreted, robust, safe, architecture neutral, portable, high performance, multithreaded, dynamic language.
Java is interesting because
  • It is both a general purpose object-oriented language along the lines of C++
  • and it is particularly designed to interface with Web pages and to enable distributed applications over the internet.
The Web is becoming the dominant software development arena; this will drive Java as the best supported, most widely taught language

HTML version of Basic Foils prepared July 6 99

Foil 5 Java is an important language in the world

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The Java Language has several good design features
  • secure, safe (wrt bugs), object-oriented, familiar (to C C++ and even Fortran programmers)
Java has a very good set of libraries covering everything from commerce, multimedia, images to math functions (under development at http://math.nist.gov/javanumerics)
Java has best available electronic and paper training and support resources, growing labor force trained in Java
Java is rapidly getting best integrated program development environments
Java naturally integrated with network and universal machine supports powerful "write once-run anywhere" model

HTML version of Basic Foils prepared July 6 99

Foil 6 Why use Java as the basis for HPCC/Scientific Computing Software?

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
HPCC is a small field and Scientific Computing a modest size field with limited resources for very hard problems and must leverage as much software as possible
Web Software provides an excellent pervasive user interface with Java Applets and WebWindows
Web Software provides a potentially excellent high performance object oriented language (Java) for scientific and engineering computation
All(!?) we need to do is to add high performance and special features of scientific computing (math libraries) to the Web!
Technology for generating parallelism (user decomposition, parallelizing compilers) not provided by web and must come from HPCC

HTML version of Basic Foils prepared July 6 99

Foil 7 The Computing Pyramid

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Bottom of Pyramid has 1000 times dollar value and compute power of best supercomputer (tip of pyramid) but supercomputer has high performance network to support close synchronization needed by classic parallel algorithms
Web Software MUST be cheaper and better than MPP software as factor of 100 more money invested!
Therefore natural strategy is to get parallel computing environment by adding synchronization of parallel algorithms to loosely coupled Web distributed computing model

HTML version of Basic Foils prepared July 6 99

Foil 8 Architecture of Java Applets

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
web server
Java code
is compiled
to produce
applet codes,
called bytecodes,
part of web
document
collection
web client, running browser
such as Netscape or IE
executes (restricted)
applet code to display
in browser window
Internet
Browsers (Netscape 2.0/3.0/4.0, HotJava, Microsoft IE ...) supporting Java allow arbitrarily sophisticated dynamic multimedia applications inserts called applets, written in Java, to be embedded in the regular HTML pages and activated on each exposure of a given page.

HTML version of Basic Foils prepared July 6 99

Foil 9 Architecture of Java Applications

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java code
is compiled
to produce
bytecodes
run by Java
interpreter
to produce
results
OR
Java code
is compiled
to produce
native code
run directly
on machine
for better
performance
Java applications are compiled and run on a machine just like any other general programming language such as C/C++. No web server or network are required although Java applications may also use network connections for distributed computing.

HTML version of Basic Foils prepared July 6 99

Foil 10 Java Development Kit (JDK)

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The Java compiler and interpreter come in a software package called the Java Development Kit.
The compiler, called javac, produces bytecodes, which is compiled code, not for any particular machine, but for the Java Virtual Machine (VM), and abstract machine definition. Thus the compiled code is architecture independent.
The interpreter, called java, executes the bytecodes for a particular machine. The semantics are carefully specified to be the same on all machines.
There is also a program primarily for debugging, called appletviewer, to view applets, a sort of mini-browser.
Currently in use are two versions:
  • JDK1.1, mature version supported by most browsers
  • JDK1.2, has additional classes, such as swing set for fancier user interfaces, but not yet supported by browsers in common use.

HTML version of Basic Foils prepared July 6 99

Foil 11 Java Applications

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
All Java programs are written into a file with a ".java" extension.
Applications are .java files with a main method which is excuted first.
How to compile and run a Java application (via bytecodes):
  • 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

HTML version of Basic Foils prepared July 6 99

Foil 12 The Simplest Java Application: Hello,World!

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables and subroutines called methods.
class HelloWorld
{ public static void main (String[] args)
{ System.out.println("Hello World!");
}
}
Each program is enclosed in a class definition.
main() is the first method that is run.
The notation class.method or package.class.method is how to refer to a public method (with some exceptions).
Syntax is similar to C - braces for blocks, semicolon after each statement. One difference: upper and lower case matter!

HTML version of Basic Foils prepared July 6 99

Foil 13 Java Applets

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
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 methods called init, start, paint etc. for displaying on the browser window
Applets are not trusted as a default, so they have several restricitions on running on the client machine
  • no printing or file I/O
  • cannot connect through the network to any machine but its own server
  • any new windows created by the applet have a warning label

HTML version of Basic Foils prepared July 6 99

Foil 14 Preparing an Applet

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The applet should be run through javac compiler getting a .class file as before: javac MyApplet.java
The resulting file MyApplet.class is then stored in the document collection of a web server (hence has a URL location).
Also create an HTML file (say MyApplet.html) with an applet tag to MyApplet.class.
When the browser loads the .html file, it will also download the .class file and invoke the java interpreter to run the init, start, and paint methods.

HTML version of Basic Foils prepared July 6 99

Foil 15 The Simplest Java Applet: Hello, World!

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java applets are part of the class hierarchy that can call methods to display on a screen (within the browser window). One way to draw on the screen is to call the method drawString from the standard method paint.
import java.awt.Graphics;
public class HelloApplet extends java.applet.Applet
{ public void paint (Graphics g)
{ g.drawString("Hello World!", 5, 25);
}
}
The import statement allows the use of methods from the Graphics class without the dot notation .
The paint method displays a graphics object on the screen - one of the standard methods that takes the place of main for applets.
Puts this as a subclass of Applet.

HTML version of Basic Foils prepared July 6 99

Foil 16 Displaying your applet from a Web page.

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
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><head>
<title>Simple Hello Page</title>
</head>
<body>
My Java applet says:
<applet code="HelloWorldApplet.class" width=150 height=25>
</applet>
</body></html>
Name of your applet class.
The browser will use a rectangle of width 150 pixels and height 25 pixels to display the applet within the other html.

HTML version of Basic Foils prepared July 6 99

Foil 17 Multi-tier Architecture

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Distributed applications on the web naturally have a multi-tier architecture.
Java plays a role at all three levels:
  • Graphical User Interface and client side analysis systems, including visualization
  • Middle layer servers and software integration, including web servers, distributed object servers and other application servers.
  • Less important for backend client software, which may be legacy code.
Middle level servers
Client user interface running through browser
Internet
Internet or proprietary network
Backend computing or databases

HTML version of Basic Foils prepared July 6 99

Foil 18 Multi-tier architecture for Scientific Computing

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index

HTML version of Basic Foils prepared July 6 99

Foil 19 Java in the three tiers of scientific computing

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java for the Graphical User Interface and client side analysis systems, including visualization: Java has unique advantages over other languages
Java for Coarse Grain Software Integration: as in collaboration and metacomputing
  • Java has unique advantages over other languages
Java as a high performance scientific language: for "inner" (and outer) loops
  • Here both parallel and sequential issues are important
  • Java as good as other languages with higher performance than C++ and advantages of object oriented programming over Fortran and C

HTML version of Basic Foils prepared July 6 99

Foil 20 Resources for the Java Programming Language

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The original resource was The Java Language Specification by Sun Microsystems, Inc., March 1995 updated to October 1995 but superceded by
  • The Java Programming Language, by Ken Arnold and James Gosling, 2nd edition, Addison-Wesley, 1998.
http://www.javasoft.com web site has plenty of references including

HTML version of Basic Foils prepared July 6 99

Foil 21 Books on Java

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
There are a multitude of Java books, many are excellent, here are two we have used for course textbooks:
  • Core Java, by Gary Cornell and Cay S. Horstmann, Sunsoft Press, Prentice-Hall, offers detailed coverage of the whole language and packages for advanced programmers. There are two volumes:
    • Core Java 2, Volume I, Fundamentals, 4th ed.
    • Core Java 1.2 Advanced Features
  • Java, How to Program, by Deitel and Deitel, Prentice-Hall, starts with beginning programming concepts and progresses rapidly through Java language. It has the most programming exercises. Third edition due out in July 99 will have Java 1.2.
The most highly recommended reference book
  • Java in a Nutshell, by David Flanagan, is the language reference book in the familiar O'Reilly series. The 2nd edition of this book is now out - it omits many examples from the first edition to make room for large section on JDK 1.1 - currently best book reference. Also the companion book Java Examples in a Nutshell..

HTML version of Basic Foils prepared July 6 99

Foil 22 Java Development Environments

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
These range from simple tools that give a windowing interface to the edit/compile/run or view cycle:
  • JavaEdit from Dick Chase on PC's
  • JADE (under development at NPAC for classes)
to the elaborate commercial development environments that can also track projects and help generate code for user interface components
  • Microsoft Visual J++
  • Symantec Café
  • Java Workshop from Sun
  • . . .

HTML version of Basic Foils prepared July 6 99

Foil 23 Details of the Java Language

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index

HTML version of Basic Foils prepared July 6 99

Foil 24 Java Language Basics

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java syntax has many similarities to C and C++.
Some differences
  • No malloc or free - it has automatic garbage collection.
  • No pointers - designers felt pointer arithmetic not robust or safe.
  • Can declare variables almost anywhere as needed.
  • No struct, union, enum, typedef from C- it has classes and objects instead.
  • Primitive types for integers and floats have machine independent semantics
    • IEEE standard definitions - same answer on all machines.
Some similarities
  • All variables must be declared
  • Syntax and comments
  • Control structures: if, while, and for statements, expressions

HTML version of Basic Foils prepared July 6 99

Foil 25 Types

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
A variable is either a primitive type such as int, float or double, or it is an object.
Note that there is no primitive type for Complex.
Type Size in bits Values: example Standard
boolean 1 true or false
char 16 \u0000 to \uFFFF ISO Unicode
byte 8 signed integer: 0
short 16 signed integer
int 32 signed integer
long 64 signed integer
float 32 floating pt: 0.0f IEEE 754
double 64 floating pt: 0.0 IEEE 754

HTML version of Basic Foils prepared July 6 99

Foil 26 Arrays

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Arrays are objects, but have a special syntax for creating and indexing, more like C and C++.
  • int states [ ]; declares the states variable to be a 1D array of ints
  • states = new int [ 128 ]; creates the new 1D array of length 128
  • states [ 27 ] = i + 1; assigning to an element
For an array of length n, indices run from 0 to n-1. There is run-time checking of indices.
Multi-dimensional arrays are arrays of arrays
  • char states [ ] [ ] = new char [12 ] [ 24 ]; delaring and creating at the same time
  • they can also be "ragged"
  • Note that different rows or columns may not be assumed to be stored contiguously in memory.
Arrays can have values that are any type of object
  • Color hues = new Color [ 1024 ];

HTML version of Basic Foils prepared July 6 99

Foil 27 Example of basic language syntax

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
public class SumArray
{
public static void main (String[] args)
{ // array declaration
int a[ ] = new int[10];
int total=0;
/* initialize a - note the use of array instance variable length */
for (int i = 0; i < a.length; i++)
{ a[i] = i * 5; }
/* sum the array */
for (int i = 0; i < a.length; i++)
total += a[i];
System.out.println( "\n" + "The sum of the array is " + total + "\n");
}
}
Familiar C operators ++ and +=
String catenation

HTML version of Basic Foils prepared July 6 99

Foil 28 Object Model Overview

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
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 instance variables 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 instance 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).
Instance
Variables
Methods

HTML version of Basic Foils prepared July 6 99

Foil 29 Class definition

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The class definition consists of
  • a header line giving the class name, modifiers, possible subclass and interface structure
  • declarations (and possibly initializations) of instance variables
  • declaration of a constructor method. This method has the same name as the class and does any initialization whenever an instance is created.
  • declarations of other methods.to perform operations using the data in the instance variable of the class

HTML version of Basic Foils prepared July 6 99

Foil 30 An example of a class definition

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
public class FAstate extends otherClass implements someInterface { // instance variables private int state; //constructors public className ( . . . parameters . . . ) { . . . initialize instance variables (perhaps with parameters) . . . } // other method declarations public int getState ( ) { return state; } public advanceState ( int inc ) { . . . Change the state, using parameter inc . . . } }

HTML version of Basic Foils prepared July 6 99

Foil 31 Inheritance (the subclass relationship)

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
In the Java class header, we put B extends A which means B 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.
Or B may give a new definition of some method of A. This is called overriding.

HTML version of Basic Foils prepared July 6 99

Foil 32 Access control

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The modifiers in the class head control how other classes can access this one:
  • public -- May be used by code outside the class package
  • private -- this class can only be used within current file
  • friendly(i.e. empty ClassModifier) -- class can be used only within current package (directory)
  • protected -- Only accessible to subclasses
These modifiers are also used for access control to the instance variables and methods.
There are other modifiers. For example, a static variable or method only has the same value for all instances of the class. A final variable cannot be assigned to (used for constants).

HTML version of Basic Foils prepared July 6 99

Foil 33 Classes are used as program modules

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Some classes, such as our hello application, have just one or more method that will be executed as a program. Only one instance of these classes is created (by the interpreter).
Other classes are used to structure data, such as the previous example. Another example might be to have a class to represent type Complex. It would have
  • variables to hold the real and imaginery parts of each instance
  • a method called Complex to initialize those variables on creation
  • methods called plus, subtract, and so on to operate on each instance of the complex class.
A computational class could create several instances of the class Complex and use them with the methods for arithmetic.

HTML version of Basic Foils prepared July 6 99

Foil 34 Packages

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
A package is a collection of classes.
Java directly associates the names of classes and packages with the directory (folder) structure:
  • A package must in be a directory or folder of that name
  • A public class must be in a file of that name. Note that a file may contain other (non-public) classes.
Packages of classes may be defined by users as well as forming the structure of the remaining Java libraries.
What we have seen so far is all there is to the base language Java (with the exception of Exceptions!). All of the rest of Java functionality comes as classes defined in packages that come with the language.

HTML version of Basic Foils prepared July 6 99

Foil 35 Using objects from Java packages

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Suppose that you want to use two Buttons in an applet. The Button class is in the package java.awt. java.awt.Button b1 = new java.awt.Button( "label" ); All objects created this way are represented internally as references.
Or you can more succintly use the import statement to open the context of the package java.awt:
import java.awt.*;
public class MyApplet extends java.applet.Applet
{ public void init ( )
{ Button b1 = new Button ( "start");
Button b2 = new Button ( "stop");
. . . b1.setLabel ( "pause");
}
}
This calls the class constructor method Button to create the new instance and initialize the label .

HTML version of Basic Foils prepared July 6 99

Foil 36 Exceptions

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Exceptions are run-time errors that may arise
  • divide by zero
  • opening a file that doesn't exist
They are handled with try and catch blocks: try { some code that may generate an exception } catch (Exception ex) { some code to handle this case }
Exceptions are described by objects which are instances or subclasses of the class Exception. You may create your own.
You don't have to handle all run-time exceptions such as divide by zero and array index out of bounds.

HTML version of Basic Foils prepared July 6 99

Foil 37 Data Structures

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java has classes for a rich set of data structures.
  • Once arrays are created, they cannot change size. But the class Vector can increase and decrease in size by adding and deleting elements.
  • Strings cannot be changed at all once they are created. The Stringbuffer class does allow to change the contents in place.
  • The class HashTable creates a structure that maps keys to values.
  • A subclass of HashTable is Properties, which can create and manipulate property lists.
  • There is a class Stack for traditional stack structures.
  • There are classes for Dates and Calendars.
  • There are classes StringTokenizer and StreamTokenizer which usefully parse strings or streams to take out floating point numbers and identifiers as single entities.

HTML version of Basic Foils prepared July 6 99

Foil 38 Abstract Windowing Toolkit (AWT)

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
This package contains all the classes to create graphical user interfaces.
It allows some primitive drawing (in a subpackage called graphics).
It has a number of components corresponding to parts of a windowing interface
  • basic components like buttons, textfields, lists, scrollbars, etc.
  • components such as panel and frame which allow you to place a number of other components within them. This is hierarchical.
There are layout managers to help arrange the components.
There are events and a model to handle events that come from user interactions such as clicking a button or typing in text. import awt.* ;
  • and import awt.event.* ;

HTML version of Basic Foils prepared July 6 99

Foil 39 Graphics Concepts

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Think of a Java applet as a graphics window on which to draw text and other objects
The graphics window has a particular width and height as specified in the <APPLET> tag
The origin is in the top left-hand corner of the graphics window: the x-coordinate increases from left to right, while the y-coordinate increases from top to bottom

HTML version of Basic Foils prepared July 6 99

Foil 40 Graphics Methods

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
There are graphics methods to draw lines, arcs, ovals, rectangles and other shapes. Here is one method:
The drawRect(...) method draws a rectangle anchored at point (x,y): public void drawRect( int x, int y, int w, int h );
For example, this can be used to draw histograms.
(x,y)
w
h

HTML version of Basic Foils prepared July 6 99

Foil 41 Basic components

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
For each basic component, one can create one or more instances of the component type and then use one of the "add" methods to place it into a Container such as an applet window.
  • Button button1 = new Button("Click here");
  • add (button1);
  • TextField tf = new TextField(Iinitial text", 30);
  • add(tf);
For each basic component, additional methods allow access to the properties of the components:
  • button1.setBackground (Color.cyan);
  • text which is displayed can be changed:
  • tf.setText("now show a new text");
  • If the user types input into the text field, it can be obtained:
  • stringvar = tf.getText();
Click here
Initial text

HTML version of Basic Foils prepared July 6 99

Foil 42 Event Model - I

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The user can interact with the GUI on many of its components, by clicking a button, typing in text, etc. These actions cause an Event to be generated, which will be reported by the system to a class which is an Event Listener, and which will have an event handler method for that event. This method will provide the appropriate response to the user's action.
An Event Listener is an instance of any class that wants to receive events.
An event source is an object that generates events.. An event source will keep a list of event listeners who want to be notified for particular events.
  • button1.addActionListener ( eventclass );

HTML version of Basic Foils prepared July 6 99

Foil 43 Event Model - II

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The event source notifies event listeners by invoking a particular method of the event listener (aka the event handler method) and passing it an Event object, which has all the information about the event.
  • public class EventClass implements ActionListener
  • {
  • public void actionPerformed ( ActionEvent ev )
  • {. . . put code here to respond to event ev . . . }
  • }
Each basic component has a defined Event class, Listener interface, and event handling method names.
Note that an interface is a class in which there are headers of methods with no method body. It just gives the name(s) of methods such as actionPerformed that the user is to provide.

HTML version of Basic Foils prepared July 6 99

Foil 44 Sketch of Event Handling applet

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
public class MadLib extends java.applet.Applet implements ActionListener
{ Button firstmadlib, clearbutton; Label prompt1,prompt2;
TextField input1, input2 ; TextArea output ;
. . . . . }
public void init ( )
{ // add labels and textfields (without events)
prompt1 = new Label ( "Enter a noun: ") ; add ( prompt1 ) ;
input1 = new TextField ( 20 ) ; add ( input1 ) ;
. . . }
public void actionPerformed (ActionEvent event)
{ if ( event.getSource ( ) == firstmadlib )
{String word1 = input1.getText ( ) ;
output.setText (word1 + . . .); . . . }
if ( event.getSource ( ) == clearbutton )
{input1.setText ( "" ) ; . . . }}

HTML version of Basic Foils prepared July 6 99

Foil 45 Layout Managers

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The various panels in a container are laid out separately in terms of their subcomponents
The arrangement is controlled by general strategies which are embodied in 5 LayoutMangers
To create a layout, such as FlowLayout, in your panel:
  • setLayout(new FlowLayout());
  • This particular Layout is the default and flows in the components in the order that the add method is called.
Other Layout Managers:
  • GridLayout - a regular rectangular grid (table)
  • BorderLayout - 5 areas as in diagram
  • GridBagLayout - general arrangement of space
  • CardLayout - card panels replace each other in the same space.

HTML version of Basic Foils prepared July 6 99

Foil 46 Additional Capabilities of the AWT

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The AWT has many additional capabilities. It can create separate windows either in the form of frames or dialog boxes.
  • Frames have menubars, with menus
  • One can dynamically make frames appear and disappear.
The "swing" set has more advanced features such as internal frames, dynamically sized tables, trees, color choosers, overlapping components, progress bars, popup menus and many other sophisticated user interface elements.

HTML version of Basic Foils prepared July 6 99

Foil 47 Multi-threading

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Threads are "light-weight" processes (unlike UNIX processes), which communicate by a combination of shared memory and message passing
  • This communication mechanism is employed naturally by Java
Java threads are limited and for those coming from an HPCC background, we note Java threads have no immediate support for some key parallel computing concepts such as distributed memory (threads running in separate operating system instances)
Java threads are based on a locking mechanism using monitors for synchronization, introduced by Hoare in 1974

HTML version of Basic Foils prepared July 6 99

Foil 48 Introducing Threads

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
One way to create threads is to write a separate class that subclasses the Thread class.
The main line of execution is put in a method called run(), which overrides the method of the same name from the Thread class.
Instances of this subclass are created like this:
  • MyThread mt = new MyThread( );
Thread control:
  • When the thread is created, it does not automatically start running. The class that creates it must call the Thread method start(). mt.start ( );
  • Other methods may be called: Thread.sleep( ), Thread.yield( ), suspend( ), resume( ), stop( ), and join( ).

HTML version of Basic Foils prepared July 6 99

Foil 49 Animation Thread for Applet

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Another way to introduce a thread is to create a thread that uses a run method in another class. This class has to be declared to implement the interface Runnable, which has one method called run.
This is the correct way to run an animation in an applet, which can show a simulation or other sequence of drawings.
We have created a template program for an applet with a drawing area for the animation and a layout with room for several control buttons, textfields, etc.

HTML version of Basic Foils prepared July 6 99

Foil 50 Synchronization of threads

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
In Java, two threads can communicate by accessing a shared variable (shared-memory model).
If two threads can both modify an object, that is, they can both execute a method that assigns to a shared variable, then the modifications must be synchronized.
This is easy - just declare the method to be synchronized!
This means that Java will ensure that only one thread executes the method at a time and is not interrupted.
Suppose more than one thread has an instance of an account:
  • public class Account
  • { int bankBalance; ...
  • public synchronized void CreditAcct(int amt)
  • { ... bankBalance += amt; ... }}

HTML version of Basic Foils prepared July 6 99

Foil 51 More about synchronization

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Often, one thread is producing some data, while another thread wants to use it.
A flag variable is introduce that both threads can check.
If the data is not ready (flag is false), the consuming thread can call the method wait ( ), which causes it to be suspended until some other event occurs.
When the producing thread has some data, it can set the flag to be true and call the method notify ( ), which causes an event that will "wake up" one of the suspended threads.
If the user doesn't get the logic right, there are possibilities for deadlock or other forms of non-progress.

HTML version of Basic Foils prepared July 6 99

Foil 52 Streams

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
A stream is a sequence of bytes or characters.
All types of streams are treated similarly, but may have different stream sources and sinks:
  • files
  • network connections
  • blocks of memory
  • threads
The most basic byte streams are InputStream and OutputStream. These classes have methods that can read or write a byte from or to a stream:
  • int read();
  • void write( int );
  • skip( long ); available(); flush(); close();
All of the above methods throw a possible IOException.
The read() and write( int ) methods "block" during transfer.

HTML version of Basic Foils prepared July 6 99

Foil 53 I/O with Streams

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
To read and write text, numbers, etc., you layer various filters on top of the basic input and output streams. These classes have additional methods to read and write data, both ascii and binary.
For example, opening a file and reading lines of text: BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( "filename.txt" ) ) ); while (( line = in.readLine() ) != null ) { buffer.append ( line + "\n"); } in.close ( );

HTML version of Basic Foils prepared July 6 99

Foil 54 Reading and Displaying data from a file

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
While applications can read/write files, view file directory structure, create file dialog boxes, and so on, applets are more resricted (without using certificates to relax security on the client machine). But an applet can read data from a file on the host that the applet was downloaded from.
We have created a template program that reads data from a file and displays it. This is the template for the Java CFD examples at NPAC.

HTML version of Basic Foils prepared July 6 99

Foil 55 Network Connections with URL's

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
A Java application can create and use URL's to anywhere; applets are restricted to URL's to their own web server.
  • String urlStr = "http://www.npac.syr.edu/"; try { URL url = new URL( urlStr ); } catch ( MalformedURLException e ) { System.err.println( "Bad URL: " + urlStr );}
A method URL.openConnection() returns an instance of class URLConnection:
  • URLConnection conn = url.openConnection();
  • conn.connect(); // open a connection
  • Now use this URLConnection object to open a stream:
  • new InputStreamReader( conn.getInputStream() ) );
Applications can also use conn.getOutputStream(), but not applets.
Note that one can connect not just to HTML files but also to CGI scripts and other web documents.

HTML version of Basic Foils prepared July 6 99

Foil 56 Sockets for Network Communication

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
A java applet, which is downloaded from a web server host, can connect via a network socket to any port on the same machine. This could be a java application acting as a server.
Java applications can connect network sockets to any host machine on the Internet
  • Socket t = new Socket("internet host name", port)
  • sockets have methods to open input and output streams
web server host machine
browser
Applet is downloaded
from web server
applet
Internet
80
nnnn
web
server
Java
application
Socket based on
TCP/IP protocol

HTML version of Basic Foils prepared July 6 99

Foil 57 Java Application Servers

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
web server
host machine
applet
Internet
nnnn
Java
application
Sockets
applet
Java
application
Java
application
Java application acting as a server
A Java application can open sockets to any number of clients
Again, note that socket communication is via streams, where the client and server agree on a message protocol to communicate .
Applets and applications acting as clients

HTML version of Basic Foils prepared July 6 99

Foil 58 Remote Method Interface (RMI)

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java RMI allows the programming of distributed applications across the Internet at a more abstract level than sockets. One Java application or applet (the client in this context) can call the methods of an instance, or object, of a class of a Java application (the server in this context) running on another host machine.
An example of Distributed Object Programming - similar to CORBA, except that CORBA allows the remote objects to be programmed in other languages.
The goal is to make calls to remote methods on the local machine have the same syntax and semantics as local calls.
Local Machine
Remote Machine
Local Java
local method
Remote Java
remote method

HTML version of Basic Foils prepared July 6 99

Foil 59 RMI Architecture

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
One key piece is a server that acts as the network lookup, a naming registry from which the client can get a reference to the remote object (the server).
Another key piece is object serialization. Parameters to and from the remote methods must be sent in a standard encoding across the network.
client
Server with
implementations
of remote methods
Naming registry
RMI
RMI
RMI
Calls to methods in the remote object may pass parameters and receive results

HTML version of Basic Foils prepared July 6 99

Foil 60 Java Web Servers/Servlets

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java is now being used to develop web servers.
Java is also being used as the language to develop server side code for web servers, using the Common Gateway Interface (CGI) of the HTTP protocol. These are called servlets.
  • Servlets follow a standard servlet API defining the interface between the server and the servlet, and are designed to work within the request/response model of HTTP. The servlet API is part of the standard extensions of JDK.
  • Servlets can provide all the services of standard CGI, but are platform-independent, i.e. they can be used with any server implementing the API, and have more state capabilities.
  • More generally, servlets play the role of providing middle-tier services between the client and the back-end applications.

HTML version of Basic Foils prepared July 6 99

Foil 61 Java DataBase Connection (JDBC)

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
JDBC provides a set of classes for Java with a standard SQL database access interface.
Provides an API for database "drivers" to make actual connections and transactions to database products.
  • includes JDBC-ODBC bridge to standard ODBC drivers, a common interface for relational databases from C
JDBC is "low-level" interface, calling SQL commands directly but is meant to be a base for higher-level interfaces.

HTML version of Basic Foils prepared July 6 99

Foil 62 JDBC Architecture

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Java application or applet with JDBC
Java applet or
HTML browser
Application Server (Java)
with JDBC
Jdbc Driver
DBMS
Two-tier Model
Jdbc Driver
DBMS
Three-tier Model
DBMS Proprietary Protocol
DBMS Proprietary Protocol

HTML version of Basic Foils prepared July 6 99

Foil 63 Connecting to the Database

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The JDBC API is in the package java.sql.
The classes include a DriverManager that keeps track of available database drivers. Download the driver you want and use it to connect to the database server.
Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection ("jdbc:oracle:thin:@carver.npac.syr.edu:1521:europe", "dbusername", "dbpassword");

HTML version of Basic Foils prepared July 6 99

Foil 64 Database queries

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
There are several classes for making SQL statements.
  • Statement stmt = conn.createStatement( ); ResultSet rs = stmt.executeQuery("select a,b,c from table1");
The query returns a result set, which has database table rows satisfying the query.
  • while (rs.next( )) { //print values for current row int i = r.getInt("a"); double f = r.getDouble("b"); String s = r.getString("c"); System.out.println("row" + i + f + s); }
SQL statements may insert, update and delete, and may execute stored procedures in the database. Additional classes deal with transaction commit and rollback.

HTML version of Basic Foils prepared July 6 99

Foil 65 JavaBeans

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
"Reusable software components that can be manipulated visually in a builder tool."
They are Java's implementation of "component-based" visual programming
This modern software engineering technique produces a new approach to libraries which become a "software component infrastructure(SCI)"
There is a visual interface to discovery of and setting of values of and information about parameters used in a particular software component
JavaBeans uses the event model of JDK1.1 to communicate between components

HTML version of Basic Foils prepared July 6 99

Foil 66 Features of JavaBeans

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index

HTML version of Basic Foils prepared July 6 99

Foil 67 Component Programming with JavaBeans

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The visual interface allows inspection of and implementation of both individual beans and their linkage (events) . This visual construction of linkage allows one to form nontrivial programs with multiple communicating components
A Java Bean component can be a simple GUI component such as a Button or a complex program with many properties and methods such as a spreadsheet.
Apart from the event mechanism used for communication and linkage, ComponentWare (and JavaBeans in particular) "just" give a set of universal rules (needed for interoperability) for rather uncontroversial (albeit good) object-oriented and visual programming practices

HTML version of Basic Foils prepared July 6 99

Foil 68 Java Grande Forum

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index

HTML version of Basic Foils prepared July 6 99

Foil 69 The Java Grande Forum

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Encourages and develops the use of Java for
  • High Performance Network Computing
  • Scientific and Engineering Computation
  • (Distributed) Modeling and Simulation
  • Parallel and Distributed Computing
  • Data Intensive Computing
  • Communication and Computing Intensive Commercial and Academic Applications
  • HPCC Computational Grids ........
Java has potential to be a better environment for "Grande application development" than any previous languages such as Fortran and C++
The Forum Goal is to develop community consensus and recommendations for either changes to Java or establishment of standards (frameworks) for "Grande" libraries and services
These Language changes or frameworks are designed to realize "best ever Grande programming environment"

HTML version of Basic Foils prepared July 6 99

Foil 70 Activities of Java Grande Forum I

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Two major working groups promoting standards and community actions
Numerics: Java as a language for mathematics led by Ron Boisvert and Roldan Pozo from NIST
  • Changes in Java controversial handling of floating point which currently has goal of reproducible results but this leads to non optimal accuracy
  • Addition of Complex types or classes
  • Lightweight classes and Operator overloading -- enables implementation of complex as a class
  • "Fortran rectangular multidimensional arrays" -- Java naturally has "arrays of arrays"
  • High quality math libraries with agreed interfaces -- FFT, Matrices, Transcendental functions

HTML version of Basic Foils prepared July 6 99

Foil 71 Activities of Java Grande Forum II

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
Distributed and Parallel Computing led by Dennis Gannon and Denis Caromel (INRIA, France)
  • Performance of RMI (Attractive Java distributed object model - "remote method invocation")
  • Performance of Java runtime (the virtual machine VM) with lots of threads, I/O, memory use
  • Parallel Computing interfaces including Java MPI binding
  • Development of universal (condor, Globus, Legion UNICORE WebSubmit ..) Java interface to computing resources -- enables seamless computing (easier than metacomputing!)
Development of Grande Application benchmarks

HTML version of Basic Foils prepared July 6 99

Foil 72 Proposal for class Complex

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
The Java Grande forum has a draft proposal for a class to implement type Complex, although there are still several difficulties in having a class instead of a primitive type:
  • syntax is methods, not operators
  • assignment is of references, not values
  • overhead of method calls may cause bad performance
The Forum is working with Sun on these issues.
One version of the constructor: Complex z = new Complex ( x, y ); // x and y are doubles
Syntax for operators: Complex z1, z2, z3; z1 = Complex.plus ( z2, z3 ) ; or z1 = z2.plus ( z3 ) ;
Many additional operators
Definitions based onC9x Annex G:"IEC 559-compatible complex arithmetic.

HTML version of Basic Foils prepared July 6 99

Foil 73 Proposal for Rectangular Array class

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
There is a rectangular array interface, which would allow multiple implementations.
Class doubleArray implements part of the interface by providing get and set methods for indexing elements.
Classes doubleArray1D, doubleArray2D, . . . implement the various ranks
One of the constructors: new doubleArray2D ( a ); takes a Java array a of type double[ ] [ ] and makes a rectangular array of the same shape and values.
Various get methods allow to select elements, rows, columns, or various slices. The implementation may store the array as a 1D object and can thus optimize these operations.

HTML version of Basic Foils prepared July 6 99

Foil 74 Proposal for Rectangular Array class

From Use of Java Language in Computational Science DoD Modernization Users Group Conference Monterey -- June 7 99. *
Full HTML Index
There is a rectangular array interface, which would allow multiple implementations.
Class doubleArray implements part of the interface by providing get and set methods for indexing elements.
Classes doubleArray1D, doubleArray2D, . . . implement the various ranks
One of the constructors: new doubleArray2D ( a ); takes a Java array a of type double[ ] [ ] and makes a rectangular array of the same shape and values.
Various get methods allow to select elements, rows, columns, or various slices. The implementation may store the array as a 1D object and can thus optimize these operations.

© 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 Tue Jul 6 1999