For example,
if the full qualified name of a class is
david.games.tetris.SoundEffects,
the full path of the class file must be
david/games/tetris/SoundEffects.class
The Java API consists of the classes defined in the eight packages :
java.applet - Classes for implementing applets
java.awt - Classes for graphics, text windows, and GUIs
java.awt.image - Classes for Image processing
java.awt.peer - Interfaces for platform independent GUI toolkit
java.io - Classes for all kinds of input/output
java.lang - Classes for the core language
java.net - Classes for networking
java.util - Classes for useful data types
There are three forms of import statements :
import package;
For example, after
import java.awt.image,
we can call image.ImageFilter
import package.class;
For example, after
import java.util.Hashtable,
we can call Hashtable.
import package.*;
Similar to the previous option, however makes all the classes in the
package available by its name.
Primitive Data Types
Java supports all the basic primitive data types :
boolean, char, byte,
short (integer), long (integer),
int, float, double.
See online tutorial or reference book for details.
Reference Data Types
For example,
By reference,
Button a,b;
p = new Button(); // p refers to a Button object
q = p; // q refers to the same Button
p.setLable("OK"); // change to the object through p
String s = q.getLable(); // q refers to the same object (p),
// hence value of 's' is "OK"
This is not true for primitive types,
int i = 3; // i contains the value 3 int j = i; // j contains a copy of the value of I i = 2 // changing I doesn't change j, now I is 2, j is 3.
Copying Objects
Button a = new Button("OK");
Button b = new Button("Cancel");
a = b;
To copy the data of one object into another object, use clone() method:
Vector b = new Vector; c = b.clone();
Only classes that implements Cloneable interface can be cloned. See
API for more info.
To copy arrays we can use System.arraycopy().
Arrays, Strings, Operators, if/else, do/while, switch, etc...
see online tutorial or reference book.
Exceptions and Exception Handling
Its a new feature in Java.
An exception is a signal that indicates that some sort of exceptional
condition (such as an error) has occurred.
To throw an exception is to signal an exceptional condition.
To catch an exception is to handle it, to take whatever actions
are necessary to recover from it.
Exceptions propagate up through the lexical block structure of a Java
method, and then up the method call stack.
Exception Handling
The try/catch/finally statement in Java handles exceptions.
try - establishes a block of code that is to have its exceptions
and abnormal exists handled.
try - block is followed by zero or more catch clauses that
catch and handle specified types of exceptions.
The catch clauses are optionally followed by a finally
block that contains clean-up code.
The statements of a finally block are guaranteed to be executed,
regardless of how the code in the try block exists.
Templete for try/catch/finally
try {
// code runs from the top to the bottom
// if required it throws exceptions
// or exit the block via break, continue, return
}
catch (SomeException e1) { // Handle an exception object e1}
catch (AnotherException e2) { // Handle an exception object e2}
finally { // Always execute this code}
For example,
In the API, the class FileOutputStream is defined as,
public FileOutputStream(String name) throws IOException
Creates an output file stream to write to the file
with the specified name.
Parameters: name - the system-dependent filename.
Throws: IOException
if the file could not be opened for writing.
Hence in the program segment we can say,
try {
fOut = new FileOutputStream("OutFile.txt");
}
catch (IOException e) {
System.err.println("Caught IOException:" + e.getMessage());
}
public class Simple extends Applet {
. . .
public void init() { . . . }
public void start() { . . . }
public void stop() { . . . }
public void destroy() { . . . }
. . .
}
The Simple applet, like every other applet, features
a subclass of the Applet class.
The Simple class overrides four Applet methods so that it can respond
to major events:
init()
To initialize the applet each time it's loaded
(or reloaded).
start()
To start the applet's execution, such as when the applet's loaded or
when the user revisits a page that contains the applet.
stop()
To stop the applet's execution, such as when the user leaves the applet's page or quits the browser.
destroy()
To perform a final cleanup in preparation for unloading.
class Simple extends Applet {
. . .
public void paint(Graphics g) { . . }
. . .
}
paint()
The basic display method. Many applets implement the paint() method to draw the applet's representation within a browser page.
update()
The default implementation of update() clears the Component's
background and after that calls paint(). You can use this method
along with paint() to improve drawing performance.
repaint()
It repaints the drawing area by calling update().
Few simple examples