Java Packages, Classes, and Directory Structure

For example,

if the full qualified name of a class is

the full path of the class file must be



Packages of the Java API

The Java API consists of the classes defined in the eight packages :



Import Statement

There are three forms of import statements :

import package;

import package.class;

import package.*;



Primitive Data Types

Java supports all the basic primitive data types :

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

To copy the data of one object into another object, use clone() method:

Only classes that implements Cloneable interface can be cloned. See API for more info.

To copy arrays we can use System.arraycopy().


Checking Objects for Equality


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

For example,

In the API, the class FileOutputStream is defined as,

Hence in the program segment we can say,


The Life Cycle of an Applet

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()
start()
stop()


destroy()

Methods for Drawing

  class Simple extends Applet {
        . . .
     public void paint(Graphics g) { .  . }
        . . .
  }

paint() 



update()

repaint() 


Few simple examples