[ ~espresso ] [ http://www.w3.org/pub/WWW/OOP/9606_Workshop/ ] [ phoneCase.html ] [ wallstreetweb ] [ index.html ] [ http://www.inch.com/~friskel/ ] [ http://www.c2.org/~andreww/javascript/ ] [ http://www.gamelan.com/ ] [ http://www.inmet.com/java.html/ ] [ algintro.html ]
See Also: New and unsorted Java Announcements [ java.www.sites.html ]
My samples:
[ http://web.csusb.edu/public/faculty/dick/ ]
Java at CSCI.CSUSB.EDU(October 1996)
We have the Java Development Kit (1.0) with its
compiler, interpreters, library packages, documenter, and disassembler
on the Sun Workstations in JB358 and Orion. The binaries, libraries etc
are in /usr/local/java. The compiler and interpreter (javac & java) figure
out where the classes are relative to this directory. I have
programmed 'Q'/'quickie' to compile, document, and interpret
Java applications for users of the Sun systems here. All the
Java tools can be run by typing in their name and the right arguments,
as long as you are on a Sun and have /usr/local/bin in your PATH
Further the
/publicdirectory on Orion is on the World Wide Web where it appears as
http://web.csusb.edu/public/See my 'how-to' guide: //www/dick/publishing
This page plus the [ CS320 Labs ] are designed to be a step by step introduction to Java for a competent programmer.
The Java Programming Language
Overview
Java can be used to write applications and applets. A Java
application is similar to any other high-level language program
in that it is compiled and then run on the same machine.
An applet is compiled on one machine, stored on a server in
binary, and is then sent to a another machine over the Internet
to be interpretted by a Java-aware browser.
An application may compile and run like any other program but it is
actually a class
[ class ]
that contains a special method called 'main'.
When the interpreter
[ java ]
is called with the name of the class,
Java looks for main(String[]) in the class and invokes it:
The HelloWorld Application
import java.lang.*;
class Hello {
public static void main(String argv[]){
System.out.println("Hello, World!");
}
}If the above is in a file called Hello.java [ Hello.java ] then it would be compiled like this:
javac Hello.javaand run by the java interpreter like this:
java HelloNotice that the file is compiled, but a class in interpreted. Compiling a Java file can generate one or more classes. The import statements allow a class to refer to other classes in other files. The compiler use these other classes's definitions to check your code, and the interpreter loads the classes binary as part of the running program.
Notice that Java code was designed to be interpreted by a
machine independent virtual machine that runs bytecode
[ bytecode ]
Automatic Documentation Tools
One nice feature of the Sun JDK are the documentation tools. A program
javadoc generates HTML documentation files from Java source code
[ javadoc] .
One HTML
file per class. Classes and parts of classes can have special documentary
comments added to them that are reformatted and incorporated into the Hypertext
produced by javadoc. This could be a boon for large scale projects
[ Doc_Comment in java.syntax ]
Another Simple Application
For an example of a simple and useless Java application and its
documentation see
[ Luv.java ]
and
[ Luv.html ]
The Disassembler
The javap program disassembles bytecode (*.class files)
and outputs a description of the what is in them
[ javap] .
A Simple Applet
An Applet
[ applet ]
is a small program that can be sent across the Internet and
interpreted on a client machine. Typically an applet consists
of a collection of classes in a file, and the file is referred
to in a Web Page
[ www.html ]
written in the HyperText Markup Language(HTML)
[ comp.html.syntax.html] .
Notice that you can not run a Java applet unless you have a WWW page that refers to it. The page needs HTML like the following to call the compiled code, in the same directory:
<APPLET CODE="ClassName.class" HEIGHT=h WIDTH=w>
Altenate text
</APPLET>where h and w are the HEIGHT and WIDTH of the box in which the applet outputs its response. The alternate text between
<APPLET...>and
</APPLET>is displayed when a browser can not handle APPLETs. For the general syntax see [ applet_in_html] . The code is the result of compiling a file called ClassName.class that contains a public class called ClassName which extends an Applet.
Hello World Applet
Here is a suitable piece of HTML to test the HelloWorld class:
<head><title>Test</title></head><body>
<APPLET CODE="HelloWorld.class" HEIGHT=150 WIDTH=150>
You can not see this brilliant Java Applet.
</APPLET>
</body>Put this in a file called:
test.HelloWorld.html
The code for the HelloWorld applet has to be a public class called HelloWorld that extends Applet and is in in a Java file called:
HelloWorld.javaHere is the Java code taken from this file:
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void init() {
resize(150,25);
}//init
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}//paint
}//HelloWorld
The Applet is compiled just like anyother program:
javac HelloWorld.javaThis will generate a set of files with extension/suffix ".class". Notice that the compiler forces you to name the file
HelloWorld.javaand the Class
HelloWorldand generates the binary bytecode in a file called
HelloWorld.class
However you can not use 'java' to run the class - it has no 'main'. Niether can 'java' "run" the test.HelloWorld.html file.
Sun has written a special program to test applets in page that is part of there Java Developer's Kit. [ JDK ] Here is how you use it to test the HelloWorld applet:
appletviewer test.HelloWorld.htmlNotice that you point it at the test page that points to the class!
On the Suns in the Sun lab you can point 'Netscape' directly at a test page:
netscape test.HelloWorld.htmlbut this does not work with Orion.
Publishing a Page and Applet
The simplest thing to do is to use
publish *HelloWorld*and all the files (the test page, the class, the code, and any documentation) are all made public. [ publishing] .
Netscape on any machine can now be pointed at your test page:
netscape /public/group/your_id/test.HelloWorld.html
Applications that Run Applets
The 'java' interpreter can run classes that have an appropriate 'main'
function:
java classnameThis special method [ method ] is called main must create a new window(a Frame) in which the applet can display its output:
public static void main(String args[]) {
Frame myWindow = new Frame("HelloWorld");
//A window that can hold a "HeloWorld" object
HelloWorld helloWorldObject = new HelloWorld();
//The object that will be put in the window
helloWorldObject.init();
//HelloWorld has now initialized it self
helloWorldObject.start();
//HelloWorld has now started running
myWindow.add("Center", helloWorldObject);
//myWindow now has HelloWorld in it's center
myWindow.resize(300, 300);
myWindow.show();
}//mainYou can download an uptodate working copy of the HelloWorld class which is both an applications and and applet from [ HelloWorld.java ]
Applets, Classes, and Bytecode
Each ".class" file encodes
[ bytecode ]
a single "class" from the file.
These classes can be executed by the "java" program or transmitted
via the world wide web to a browser that runs them.
It is said that if the page is in directory with path name D on the server
and your operating system uses S in path names
( S.DOS=`\`, S.UNIX=`/`, S.Mac=`:`)then the applet code for class C must be in directory DSclasses and have a name C.class. However Netscape 2.01 for Suns looks in directory D, unless you add the attribute CODEBASE=classes to the Applet tag.
Documentation
[ http://java.sun.com/ ]
or equivalently
[ http://www.javasoft.com/ ]
or the (partial mirror)
[ http://www.applets.com/ ]
More General Information
For more general information see the Java and HotJava Documentation
[ documentation.html ]
and in particular the goals of Java
[ index.html ]
and the Java Tutorial at Sun.com
[ http://java.sun.com/tutorial/ ]
or download
//ftp.javasoft.com/docs/tutorial.html.zip
and unzip it into a HTML page.
Programmers need Sun's documentation [ Documentation ] and may find a programmer's guide [ index.html ] helpful.
For details see below and
[ See Also ]
Glossary
Also see
[ java.glossary.html ]
Also see
[ java.glossary.html ]
Syntax
UNIX
comp.lang.javascript
Specification of Java
There is a short glossary of the terms used in Java:
[ java.glossary.html ]
"The Java Language Specification" Version 1.0 beta is at JavaSoft
in compressed Postscript
//ftp.javasoft.com/docs/javaspec.ps.tar.Z
and zipped postscript
//ftp.javasoft.com/docs/javaspec.ps.zip
Sun holds older versions (I guess). and there is a WWW version of
the 1.0alpha documents
[ javaspec_1.html ]
Syntax of Java
A short summary(1.0alpha) can be found at
[ HEADING75 in javaspec_11 ]
, . . . and locally
[ java.syntax.html ]
"The Java Language Specification" [ Specification ] has a complete and up to date grammar.
For a Lex/Yacc version of the syntax see guavac
[ guavac] .
For a Java compiler-compiler see the Constructor of Useful
Parsers, put out by Scott Hudson of Georgia Tech
[ home.html ]
Semantics
Java has its meaning defined by a compiler
into a bytecoded virtual
machine, plus a specification of the virtual machine.
These were once held at
http://java.sun.com/doc/vmspec/html/vmspec-1.html
or
http://java.sun.com/doc/vmspec/VMSpec.ps.
I'm looking for up-to-date URLs...
My own notes are under construction
[ java.semantics.html ]
File Formats
A specification of the format of a .class file was held at
http://www.javasoft.com/1.0alpha3/doc/vmspec/vmspec_38.html.
Also see
[ Trivia ]
Pre_Defined Classes
Java by itself is a smallish language. The predefined classes
give it a lot more power. These are grouped into
packages in the Java run-time library:
[ packages.html] .
For more see
[ java.classes.html ]
[ java.class.tree.html ]
and
[ 1.0.2 ]
Standardization
ISO is now involved in co-ordinating the development of a standard
version of Java, see
[ http://www.dkuug.dk/JTC1/SC22/JSG/ ]
Examples
Team Java has a page of pointers to Collections of Applets [ #applets] . Also see Earthweb's Java Directory [ http://www.gamelan.com/ ] More T B A
See Also
The Source Code
Start searching here:
[ http://www.javasoft.com/ ]
News
[ http://www.cnet.com/Content/News/ ]
Announcements
The Java announcements archive
[ http://wuarchive.wustl.edu/packages/java/ ]
Local archives of announcements:
[ java.www.sites.html ]
(after July 29th 1996)
[ old.java.www.sites.html ]
(before July 29th 1996)
FAQs, Bugs, Features, and Directories
The Java Newbie FAQs from Usenet comp.lang.java:
//www.csci.csusb.edu/doc/java.newbie.FAQ
Elliotte Rusty Harold's Official Frequently Asked Questions [ javafaq.html] .
Also see TeamJAVA [ http://www.teamjava.com:80/links/ ] and HowDoI at Digital Focus: [ howdoi.html ] Also see Earthweb's Java Directory on www.Gamelan.com [ http://www.gamelan.com/ ]
Bugs and requested features page: [ bugsandfeatures.html ] at Java.soft.sun.com
I culled a few common questions and answers in [ java.FAQ ] but these are very rough and ready as yet, and not official FAQs for Java. Also see the uneditted version: [ java.mbox ]
Other Tutorials
At Fairfield:
[ java.html ]
Or download, unzip, and browse thru Sun's tutorial: //ftp.javasoft.com/docs/tutorial.html.zip
Marty Hall's Tutorials [ #Tutorials ]
(CS320 Labs): These take you step-by-step
through a series of Java Applets and Programs from your
first Applet to using the AWT to do graphics and layouts:
[ lab1.html ]
[ lab2.html ]
[ lab3.html ]
Books
Errata for "Java in 21 Days"
[ errata.html ]
Examples etc for "Java in a Nutshell" //ftp.ora.com/published/oreilly/nutshell/java/
Newly mentioned Java sites on the WWW [ java.www.sites.html ] , . . . information on objects in general [ objects.html ] , . . . information on language including Java [ Java in languages ] , . . . and notes and links about software engineering in general [ se.www.sites.html ]
Finally, the pick of the comp.lang.java Usenet news: //www/dick/samples/java.mbox
Trivia
CAFEBABE
On UNIX a compiled class can be recognized because it will
contain the magic string of bytes that spell out in hexadecimal
0xCAFEBABE.
The Naming of Java
Some think
it means "Just Another Vague Acronym".
The name "Java" is not an acronym. The original name was "Oak", after the tree outside the developers window, but it turned out that "Oak" was already copyrighted/trademarked/in-use. The descriptions of the next stage vary but all indicate some kind of brainstorming session and/or a local coffee shop, during which name "Java" was proposed and accepted.