Java Class 1 Review
-
Java programs are a psuedo machine code called "Java byte-code" usually
executed by a virtual machine (The JVM is an emulator which runs the byte
code.) I say usually because SUN has actually created hardware chips which
execute the Java byte-code instructions.
-
Java program modules are created by writing "class definitions" in "xyz.java"
source files and compiling them with a Java compiler to produce "xyz.class"
Java byte-code files.
-
There are two types of Java virtual machines (JVMs) which run the two types
of Java programs.
-
Java "applications" are run by starting the JVM (usually java.exe) and
giving it the name of a Java class file that has an entry point called
"main".
-
Java "applets" are run in a special JVM (usually part of a web browser)
by pointing it to an HTML document which has the startup info for the Java
applet. Applets are not called at entry point "main" but have several special
entry points called by the JVM when certain events occur:
-
init - is called when the applet is first loaded.
-
start - is called when the applet is made visible in the browser/viewer
window.
-
stop - is called when the applet is no longer visible.
-
destroy - is called when the applet is removed (eg the browser changes
documents).
-
paint - is called when the browser wants the applet to refresh it's output
display.
-
there are some others (note: you can write a Java pgm that runs as an app
or applet.).
-
The Java Developer Kit (JDK) has a Java compiler (javac), a Java interpreter
(the JVM - java), the basic Java class libraries, an applet testing program
(appletviewer), and some other stuff. These are command line utilities.
-
Classes are Java files that contain data items (aka variables/properties/values...)
and methods (aka code blocks/functions/subroutines...). In order to use
the variables and functions, you usually need to create an object from
the class.
-
Objects are "instances" of "classes" that have been loaded and initialized,
sort of like a tear off note from a sticky pad. They are quite useful but
hide the fact that code is still executed sequentially in some order. They
differ from mainframe started tasks or services/daemons on pc servers/minicomputers
because they may not have an independent "thread" or execution process
- they may simply reside in memory, waiting to be called by another task.
-
Note that Java programs do not actually require objects - a Java application
starts by running a "main" procedure which exists independently of any
object and before any objects which may be created by it. Therefore a better
way of looking at applications than the Java designers seem to have is
to consider the main Java procedure to be a simple script which may or
may not create objects from class files. Consider the following NetRexx
program which computes and displays the perfect squares from 1 to 10000
but creates and uses no objects:
loop R=1 to 100
say R "squared is" R*R
end
NetRexx automatically converts this to a class as Java requires, but it
is obvious this is a simple code procedure in contrast to the Java equivalent
version:
public class Squares {
static int R ;
public static void main(String[] arg) {
for (R=1;R<=100;R++) System.out.println(R + " squared is " + R*R);
} }