Images

Core Java - PART III


Core Java
Core Java offers a total immersion approach for C\C++ programmer wanting to learn java.Core Java
The course will use JDK(Java Development Kit) and covers all the new language features.
This course inclues worked out examples illustrating new JDKfeatures that are fully explained thoughout the course.
Core Java has been carefully planned to help students create faster, smaller and more efficient Java application.


Class Object and Method
Creating Classes
The diagram lists the class and identifies the structure of the code.
    

Class Object and Method
The Class Declaration
public
The public modifier declares that the class can be used by any class regardless of its package.
abstract
Declares that the class cannot be instantiated.
final
Declares that the class cannot be subclassed.
class Name Of Class
The class keyword indicates to the compiler that this is a class declaration and that the name of the class is NameOfClass.
extends Super
The extends clause identifies Super as the superclass of the class, thereby inserting the class within the class hierarchy
implements Interfaces
To declare that our class implements one or more interfaces, use the keyword implements followed by a comma-delimited list of the names of the interfaces implemented by the class
    

Class Object and Method
The Class Body
The class body contains all of the code that provides for the life cycle of the objects created from it:
1. constructors for initializing new objects,
2. declarations for the variables that provide the state of the class and its objects,
3. methods to implement the behavior of the class and its objects,
4. And a finalize method to provide for cleaning up an object after it has done its job.
Variables and methods collectively are called members.
Constructors are not methods. Nor are they members.
  

Class Object and Method
Constructors for Classes
All Java classes have constructors that are used to initialize a new object of that type.
A constructor has the same name as the class.
For example,
The name of the Stack class's constructor is Stack, the name of the Rectangle class's constructor is Rectangle, and the name of the Thread class's constructor is Thread. Stack defines a single constructor:
public Stack()
{
items = new Vector(10);
}
A constructor uses its arguments to initialize the new object's state. When creating an object, choose the constructor whose arguments best reflect how we want to initialize the new object.
When declaring constructors for our class, we use the following access specifiers in the constructor declaration to specify what other objects can create instances of our class:
private
No other class can instantiate our class. Our class may contain public class methods (sometimes called factory methods), and those methods can construct an object and return it, but no other classes can.
protected
Only subclasses of our class can create instances of it.
public
Any class can create an instance of our class.
package
Only classes within the same package as our class can construct an instance of it. Constructors provide a way to initialize a new object.
    

Class Object and Method
Implementing Methods
Methods
1. Are named modules of code
2. May optionally receive one or more parameters (arguments)
3. May optionally return a single value
4. Are associated with either a class or an object of a class. The former are called "class methods" and are the focus of this lesson. The latter are called "instance methods" and will be covered in a later lesson.
Calling a class method
• Involves the following general syntax for the call expression:
class-name.method-name(arguments)
If the method is defined within the current class, the class name may be omitted and the general syntax for the call expression is:
method-name(arguments)
• Can occur anywhere the returned data type makes sense. Methods that return no value are     called in a stand-alone statement. Methods that return a value are called within another     expression.
This method pushes an object, the one passed in as an argument, onto the top of the stack, and returns it.
Like a class, a method has two major parts:
1. method declaration
2. And method body.
The method declaration defines all of the method's attributes, such as access level, return type, name, and arguments, as illustrated here:
The method body is where all the action takes place. It contains the Java instructions that implement the method.
The Method Declaration
At minimum, a method declaration has a name and a return type indicating the data type of the value returned by the method:
returnType methodName()
{
. . .
}

Class Object and Method
The Method Body
The method body is where all of the action of a method takes place; the method body contains all of the legal Java instructions that implement the method.
Returning a Value from a Method
We declare a method's return type in its method declaration. Within the body of the method, we use the return operator to return the value. Any method that is not declared void must contain a return statement. The Stack class declares the isEmpty method, which returns a boolean:
public boolean isEmpty()
{
if (items.size() == 0)
return true;
else
return false;
}
The data type of the return value must match the method's return type; we can't return an Object type from a method declared to return an integer.
The isEmpty method returns either the boolean value true or false, depending on the outcome of a test.
The isEmpty method returns a primitive type.
Methods also can return a reference type.
For example,
Stack declares the pop method that returns the Object reference type:
public synchronized Object pop()
{
int len = items.size();
Object obj = null;
if (len == 0)
throw new EmptyStackException();
obj = items.elementAt(len - 1);
items.removeElementAt(len - 1);
return obj;
}

Class Object and Method
A Method's Name
Java supports method name overloading so that multiple methods can share the same name.
For example
class DataRenderer
{
void draw(String s)
{
. . .
}

void draw(int i)
{
. . .
}

void draw(float f)
{
. . .
}
}
Overloaded methods are differentiated by the number and type of the arguments passed into the method.
In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.
We cannot declare more than one method with the same name and the same number and type of arguments because the compiler cannot differentiate them. So, draw(String s) and draw(String t) are identical and result in a compiler error.
A class may override a method in its superclass. The overriding method must have the same name, return type, and parameter list as the method it overrides.

Class Object and Method
Example of the Math class
Different methods in the Math Class:
Sample programs
The following small samples will help us understand how easy it is to use the class methods of the Math class:
Example 1:
Performing some miscellaneous mathematical calculations on a number
public class AppMath
{
public static void main(String[] args)
{

double x;
char again;

do
{
Utility.separator(50, '~');
System.out.print("Enter a number: ");
x = Keyboard.readDouble();
Utility.skip();
System.out.println(" Ceiling = " + Math.ceil(x));
System.out.println(" Floor = " + Math.floor(x));
System.out.println(" Rounded = " + Math.round(x));
System.out.println(" Squared = " + Math.pow(x, 2));
System.out.println(" Square root = " + Math.sqrt(x));
Utility.skip();
System.out.print("Again? (Y/N): ");
again = Keyboard.readChar();
}
while (again == 'Y' || again == 'y');
}
}

Array and String
Arrays
Introduction
An array is a collection of items. Each slot in the array can hold an object or a primitive value. Arrays in Java are objects that can be treated just like other objects in the language.
An array is an indexed table of like values. While they can exist in more than one dimension, we are only interested in one-dimensional arrays as depicted by the following:
This array consists of five elements where each is an integer value (217, 138, 92, 12, and 168).
To access a particular element within an array, its corresponding index (offset) is used. For example, the integer value at index location 3 within this array is 12. Notice that the indexes are zero based because the offset of the first element is always zero (you don't have to skip over any other elements to get there).
While all programming languages include the ability to process arrays, Java provides enhanced integrity and other features not found in other languages.

Array and String
Java arrays
Java array require that all elements be of the same data type.
For example,
We may create an array of char values, or an array of float values, or an array of boolean values, etc...
Java array are objects and must be declared like one.
The general syntax is as follows:
data-type[] identifier = new data-type[n];
or
data-type identifier[] = new data-type[n];
Where identifier is the array (object) reference and n is an integer expression that specifies how many elements are in the array.
For example,
int[] myArray = new int[5];
Alternatively, a programmer may split the definition of the array reference and the instantiation of the array into two statements as follows:
int[] myArray;
myArray = new int[5];
Using either technique, it is important to understand what is created.
The following diagram may help:
The array reference (myArray) can be thought of as pointing to the array whose elements reside on the memory heap.
Notes:
1. At the time an array reference is declared, it initially contains null (it doesn't point to     anything). It doesn't receive a value until an array is assigned to it.
2. An array reference can only be assigned an array of the correct type. In the above     example, myArray is declared to be a reference to an int array, so only an array of int     values can ever be assigned to it. To attempt otherwise will result in a compile error.
3. An array reference may be reused to point to a different array (of the appropriate type).     Like all objects, however, an array that can no longer be r eferenced will be garbage     collected.
4. When instantiated, all array elements are automatically initialized to binary zeros. This     means all elements of numeric arrays will be zero, all elements of boolean arrays will be     false, and all elements of char arrays will be the null character.
Java array can be constructed from a value list.
This is an alternative construction technique as shown by
char[] chars = {'a', 'b', 'c'};
This creates a three element array of characters. The first element is initialized to 'a', the second is initialized to 'b', and the third is initialized to 'c'.
Notes:
1. The number of elements in the array is determined by the number of values in the list     and the new keyword isn't coded.
2. When a value list is used, it can only be coded in the statement that declares the array     reference.
In other words, the following will not compile:
char[] chars;
chars = {'a', 'b', 'c'};
Java array permit an element to be accessed via its unique index.
For example,
int[] numbers = {12, 15, 3, 8};
The following expression will reference the third element (an int with a value of 3)
numbers[2]
Notes:
1. An index must be int or able to be widened to int to avoid a compile error. It may not be     boolean, long, float, or double.
2. If an index is negative of exceeds the maximum valid index for the array, a runtime     error occurs. The JVM will throw an ArrayIndexOutOfBoundsException. Catching and     processing such exceptions is a topic in advanced Java.
Java array have a publicly available length field. This is an int constant representing the number of elements within the array. It is extremely useful when coding a loop to process all the elements within an array.
For example,
The following small program creates an array, loads it with some random numbers, and displays the array's contents:
public class AppArr
{
public static void main(String[] args)
{
double[] values = new double[10];
for (int i = 0; i < values.length; i++)
{
values[i] = Math.random();
}
for (int i = 0; i < values.length; i++)
{
System.out.println(values[i]);
}
}
}
Notes:
1. In an array object, length is a field and NOT a method. A common mistake is to code     length() as you would with a String or StringBuffer object and get a compile error.
2. Because the for loops are limited by the length of the array, the code is very flexible.     Changing the number of elements in the array declaration is all that is needed to work     with a different sized array.

Array and String
Object arrays
Object array are declared with the class name as the type.
For example
String[] names = new String[5];
Constructs an array of 5 String object references. It is important to understand that no String objects are created by this declaration. We have simply created an array of null object references which may later be assigned to individual String objects (as shown by the following diagram).
The statement
names[3] = new String("Hello World!");
would instantiate a String object and assign it to the fourth element in the names array (which would look like the following).
To display the value of this particular String object, one might code
System.out.println(names[3]);
which retrieves the String object referenced by the fourth element in the names array.
Object array are arrays of object references. Each element is either the reference of an instantiated object or is null. The general syntax to access an instance method of the object being referenced is
array-identifier[index].method-name(arguments)
For example,
To determine the length of the encapsulated string in the fourth element of the names array one would code the expression
names[3].length()
Object array can result in runtime errors if improperly used. In addition to an ArrayIndexOutOfBoundsException, it is possible for a NullPointerException to occur if an attempt is made to reference an object when the object reference is null.
Example:
Attempting to call an instance method of an object that doesn't exist.
public class AppObj
{
public static void main(String[] args)
{
String[] names = new String[5];
System.out.println("Length of first string: " + names[0].length());
}
}
Object array can be constructed from a value list. This requires the instantiation or the existence of the objects to be referenced by each array element.
For example,
String aString = new String("def");
String[] x = {new String("abc"), aString, "xyz"};
creates a three element array of String object references. The first element references a String object having the value " abc ", the second element references a String object having the value " def ", and the third references the String object in the literal pool having the value " xyz ".

Array and String
Strings
A sequence of character data is called a string and is implemented in the Java environment by the String class (a member of the java.lang package). The character-counting program uses Strings in two different places. The first is in the definition of the main() method:
String[] args
This code explicitly declares an array, named args, that contains String objects. The empty brackets indicate that the length of the array is unknown at compilation time because the array is passed in at runtime.
The second use of Strings in the example program is these two uses of literal strings
(a string of characters between double quotation marks " and "):
"Input has "
. . .
" chars."
The compiler implicitly allocates a String object when it encounters a literal string. So, the program implicitly allocates two String objects one for each of the two literal strings shown previously.
String String[] arrayOfStrings = new String[10];
The elements in this array are reference types, that is, each element contains a reference to a String object.
At this point, enough memory has been allocated to contain the String references, but no memory has been allocated for the Strings themselves. If we attempted to access one of arrayOfStrings elements at this point, we would get a NullPointerException because the array is empty and contains no Strings and no String objects.
We have to allocate the actual String objects separately:
for (int i = 0; i < arrayOfStrings.length; i ++)
{
arrayOfStrings[i] = new String("Hello " + i);
objects are immutable--that is, they cannot be changed once they've been created. The java.lang package provides a different class, StringBuffer, which we can use to create and manipulate character data on the fly.

Array and String
String Concatenation
Java lets us concatenate strings together easily using the + operator. The example program uses this feature of the Java language to print its output. The following code snippet concatenates three strings together to produce its output:
"Input has " + count + " chars."
Two of the strings concatenated together are literal strings: "Input has " and " chars." The third string--the one in the middle--is actually an integer that first gets converted to a string and then is concatenated to the others.

Inheritance
Introduction
Inheritance is a concept in object-oriented programming where all classes are arranged in a strict hierarchy. Each class in the hierarchy has superclasses (classes above it in the hierarchy) and any number of subclasses (classes below it in the hierarchy). Subclasses inherit attributes and behavior from their superclasses
In Java, as in object-oriented programming languages, classes can be derived from other classes.
The derived class (the class that is derived from another class) is called a subclass.
The class from which its derived is called the superclass.
In fact, in Java, all classes must be derived from some class.
The top-most class, the class from which all other classes are derived, is the Object class defined in java.lang.
Object is the root of a hierarchy of classes.
The subclass inherits state and behavior in the form of variables and methods from its superclass.
The subclass can just use the items inherited from its superclass as is, or the subclass can modify or override it.
Definition:
A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors.

Inheritance
Creating Subclasses
For example,
suppose that we wanted to create a subclass named SubClass of another class named SuperClass. We would write:
class SubClass extends SuperClass
{
. . .
}
This declares that SubClass is the subclass of the Superclass class. It also implicitly declares that SuperClass is the superclass of SubClass. A subclass also inherits variables and methods from its superclass's superclass, and so on up the inheritance tree.
A Java class can have only one direct superclass.
Java does not support multiple inheritance.
Creating a subclass can be as simple as including the extends clause in your class declaration.

Inheritance
Member Variables In Subclass Inherit?
Rule:
A subclass inherits all of the member variables within its superclass that are accessible to that subclass (unless the member variable is hidden by the subclass).
That is, subclasses
1. inherit those member variables declared as public or protected
2. inherit those member variables declared with no access specifier as long as the subclass     is in the same package as the superclass. These variables are sometimes known as     "friendly".
3. don't inherit a superclass's member variable if the subclass declares a member variable     using the same name. The subclass's member variable is said to hide the member     variable in the superclass.
4. don't inherit private member variables

Inheritance
Hiding Member Variables
The member variables defined in the subclass hide member variables of the same name in the superclass.
One interesting feature of Java member variables is that a class can access a hidden member variable through its superclass.
Consider this superclass and subclass pair:
class Super
{
Number aNumber;
}
class Sub extends Super
{
Float aNumber;
}
The aNumber variable in Sub hides aNumber in Super. But we can access aNumber from the superclass with:
super.aNumber
super is a Java language keyword that allows a method to refer to hidden variables and overriden methods of the superclass.

Inheritance
Methods In Subclass Inherit?
The rule that specifies which methods get inherited by a subclass is similar to that for member variables.
Rule:
A subclass inherits all of the methods within its superclass that are accessible to that subclass (unless the method is overriden by the subclass).
That is, subclasses
1. inherit those methods declared as public or protected
2. inherit those methods declared with no access specifier as long as the subclass is in the same package as the superclass
3. don't inherit a superclass's method if the subclass declares a method using the same name. The method in the subclass is said to override the one in the superclass.
4. don't inherit private methods

Inheritance
Overriding Methods
A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.

Inheritance
Methods a Subclass Cannot Override
A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overriden).
If you attempt to override a final method, the compiler will display an error message similar to this one and refuse to compile the program:
FinalTest.java:7: Final methods can't be overriden. Method void iamfinal() is final in class ClassWithFinalMethod.
void iamfinal() { ^ 1 error

Inheritance
Methods a Subclass Must Override
Subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract.
ABSTRACT CLASSES
Abstract classes are those which can be used for creation of objects. However their methods and constructors can be used by the child or extended class. The need for abstract classes is that you can generalize the super class from which child classes can share its methods. The subclass of an abstract class which can create an object is called as "concrete class".
Following is an example
Abstract class A
{
abstract void method1();
void method2()
{
System.out.println("this is concrete method");
}
}
class B extends A
{
void method1()
{
System.out.println("B is implementation of method1");
}
}
class demo
{
public static void main(String arg[])
{
B b=new B();
b.method1();
b.method2();
}
}

Inheritance
The Benefits of Inheritance
• Subclasses provide specialized behaviors from the basis of common elements provided by   the superclass. Through the use of inheritance, programmers can reuse the code in the   superclass many times.
• Programmers can implement superclasses called abstract classes that define "generic"   behaviors. The abstract superclass defines and may partially implement the behavior but   much of the class is undefined and unimplemented. Other programmers fill in the   details with specialized subclasses.

Inheritance
Interfaces
An interface is a collection of method names, without definitions, that can be added to classes to provide additional behavior not included with those methods the class defined itself or inherited from its superclasses.
The problem with multiple inheritance is that it makes a programming language far more complex to learn, to use, and to implement.
A Java interface is a collection of abstract behavior that can be mixed into any class to add to that class behavior that is not supplied by its superclasses. Specifically, a Java interface contains nothing but abstract method definitions and constants-no instance variables and no method implementations.
Interfaces are implemented and used throughout the Java class library whenever a behavior is expected to be implemented by a number of disparate classes.
The Java class hierarchy, for example, defines and uses the interfaces java.lang.Runnable
java.util.Enumeration,
java.util.Observable,
java.awt.image.ImageConsumer, and java.awt.image.ImageProducer.

Inheritance
Interfaces and Classes
Interfaces, like classes, are declared in source files, one interface to a file. Like classes, they also are compiled using the Java compiler into .class files.
Interfaces complement and extend the power of classes, and the two can be treated almost exactly the same.
One of the few differences between them is that an interface cannot be instantiated: new can only create an instance of a class.

Inheritance
Implementing and Using Interfaces
The implements Keyword
To use an interface, we include the implements keyword as part of our class definition
// java.applet.Applet is the superclass

public class Neko extends java.applet.Applet

implements Runnable
{ // but it also has Runnable behavior
...
}
After our class implements an interface, subclasses of our class will inherit those new methods (and can override or overload them) just as if our superclass had actually defined them.
If your class inherits from a superclass that implements a given interface, we don't have to include the implements keyword in your own class definition.
Example-
interface Fruitlike
{
void decay();
void squish();
. . .
}

class Fruit implements Fruitlike
{
private Color myColor;
private int daysTilIRot;
. . .
}

interface Spherelike
{
void toss();
void rotate();
. . .
}

class Orange extends Fruit implements Spherelike
{
. . . // toss()ing may squish() me (unique to me)
}
Here the class Orange doesn't have to say implements Fruitlike because, by extending Fruit, it already has!
Other Example-
class Sphere implements Spherelike
{ // extends Object
private float radius;
. . .
}

class Orange extends Sphere implements Fruitlike
{
. .// users of Orange never need know about the change!
}

Inheritance
Implementing Multiple Interfaces
To include multiple interfaces in a class, just separate their names with commas:
public class Neko extends java.applet.Applet

implements Runnable, Eatable, Sortable, Observable
{
...
}

Inheritance
Creating and Extending Interfaces
New Interfaces
To create a new interface,we declare it like this:
public interface Growable
{
...
}
This is, effectively, the same as a class definition, with the word interface replacing the word class. Inside the interface definition we have methods and constants.
The method definitions inside the interface are public and abstract methods; We cannot declare a method inside an interface to be either private or protected.
So, for example, here's a Growable interface with one method explicitly declared public and abstract (growIt()) and one implicitly declared as such (growItBigger())
public interface Growable
{
public abstract void growIt(); //explicity public and abstract
void growItBigger(); // effectively public and abstract
}
In addition to methods, interfaces can also have variables, but those variables must be declared public, static, and final (making them constant).
As with methods, we can explicitly define a variable to be public, static, and final.
Here's that same Growable definition with two new variables:
public interface Growable
{
public static final int increment = 10;
long maxnum = 1000000; // becomes public static and final

public abstract void growIt(); //explicitly public and abstract
void growItBigger(); // effectively public and abstract
}
Interfaces, like classes, can belong to a package by adding a package statement to the first line of the class file.
Interfaces can also import other interfaces and classes from other packages, just as classes can.

Inheritance
Methods Inside Interfaces
An actual implementation for the method in a class:
public class Orange extends Fruit
{

public germinate(Fruitlike self)
{
Orange theOrange = (Orange)self;
...
}
}

Inheritance
Extending Interfaces
When one interface inherits from another interface, that "subinterface" acquires all the method definitions and constants that its "superinterface" defined.
To extend an interface, we use the extends keyword just as you do in a class definition:
public interface Fruitlike extends Foodlike
{
...
}
In multiply inherited interfaces, the rules for managing method name conflicts are the same as for classes that use multiple interfaces; methods that differ only in return type will result in a compiler error.

Package
Introduction
Java provides a powerful means of grouping related classes and interfaces together in a single unit: packages
Packages are groups of related classes and interfaces.
Packages provide a convenient mechanism for managing a large group of classes and interfaces while avoiding potential naming conflicts.
Packages are useful for several broad reasons:
• They allow us to organize your classes into units. Just as we have folders or directories on    your hard disk to organize your files and applications, packages allow us to organize your    classes into groups so that we only use what we need for each program.
• They reduce problems with conflicts in names. As the number of Java classes grows, so    does the likelihood that we'll use the same class name as someone else, opening up the    possibility of naming clashes and errors if you try to integrate groups of classes into a    single program. Packages allow you to "hide" classes so that conflicts can be avoided.
• They allow you to protect classes, variables, and methods in larger ways than on a    class-by-class basis, as you learned yesterday. we'll learn more about protections with    packages later today.
• They can be used to identify your classes. For example, if you implemented a set of    classes to perform some purpose, we could name a package of those classes with a    unique identifier that identifies you or your organization.
The Java API itself is implemented as a group of packages.

Package
Declaring Packages
The syntax for the package statement:
package Identifier;
This statement must be placed at the beginning of a compilation unit (a single source file), before any class declarations.
Every class located in a compilation unit with a package statement is considered part of that package
Packages can be nested within other packages. When this is done, the Java interpreter expects the directory structure containing the executable classes to match the package hierarchy.

Package
Importing Packages
The import statement enables us to import classes from other packages into a compilation unit. We can import individual classes or entire packages of classes at the same time if we want.
The syntax for the import statement follows:
import Identifier;
Identifier is the name of the class or package of classes we are importing.
Other Example:
import java.awt.Color;
import java.awt.*;
The first statement imports the specific class Color, which is located in the java.awt package. The second statement imports all the classes in the java.awt package.
Note that the following statement doesn't work:
import java.*;
This statement doesn't work because we can't import nested packages with the * specification. This wildcard works only when importing all the classes in a particular package, which is still very useful.
There is one other way to import objects from other packages: explicit package referencing.
By explicitly referencing the package name each time we use an object, we can avoid using an import statement. Using this technique, the declaration of the color member variable look like this:
java.awt.Color color;

Package
Creating Our Own Packages
The first step is to decide what the name of our package is going to be.
The name we choose for our package depends on how we are going to be using those classes.
Step two in creating packages is to create a directory structure on our disk that matches the package name.
If your package has just one name (mypackage), we'll only have to create a directory for that one name.
If the package name has several parts, however, we'll have to create directories within directories.
For the package name edu.nonsense.eng.fooblitzky, we'll need to create an edu directory and then create a nonsense directory inside edu, an eng directory inside nonsense, and a fooblitzky directory inside eng. Our classes and source files can then go inside the fooblitzky directory.
Use package to Add Our Class to a Package
The final step to putting our class inside packages is to add the package command to our source files.
The package command says "this class goes inside this package," and is used like this:
package myclasses;
package edu.nonsense.eng.fooblitzky;
package java.awt;

Package
The Java Language Package
The Different Java Packages
Eight packages comprise the standard Java development environment.
The Java language package, also known as java.lang, contains classes that are core to the Java language. The classes in this package are grouped as follow:
Object
The grand-daddy of all classes--the class from which all others inherit.
Data Type Wrappers
A collection of classes used to wrap variables of a primitive data type: Boolean, Character, Double, Float, Integer and Long. Each of these classes are subclasses of the abstract class Number.
Strings
Two classes that implement character data. is a thorough lesson on the use of both types of strings.
System and Runtime
These two classes provide let your programs use system resources. System provides a system-independent programming interface to system resources and Runtime gives you direct system-specific access to the runtime environment.
Threads
The Thread, ThreadDeath and ThreadGroup classes implement the multi-threading capabilities so important to the Java language. The java.lang package also defines the Runnable interface. Runnable makes it convenient for Java class to be active without subclassing the Thread class.
Classes
The Class class provides a runtime description of a class and the ClassLoader class allows you to load classes into your program during runtime.
Math
A library of math routines and values such as pi.
Exceptions, Errors and Throwable
When an error occurs in a Java program, the program throws an object which indicates what the problem was and the state of the interpreter when the error occurred. Only objects that derive from the Throwable class can be thrown. There are two main subclasses of Throwable: Exception and Error. Exceptions are a form of Throwable that "normal" programs may try to catch. Errors are used for more catastophic errors--normal programs should not catch errors. The java.lang package contains the Throwable, Exception and Error classes, and numerous subclasses of Exception and Error that represent specific problems.
Processes
Process objects represent the system process that is created when you use Runtime to execute system commands. The java.lang packages defines and implements the generic Process class.
The compiler automatically imports this package for us. No other packages are automatically imported.

Package
The Java I/O Package
The Java I/O Package (java.io) provides a set of input and output streams used to read and write data to files or other input and output sources.

Package
The Java Utility Package
This Java package, java.util, contains a collection of utility classes. Among them are several generic data structures (Dictionary, Stack, Vector, Hashtable) a useful object for tokenizing a string and another for manipulating calendar dates. The java.util package also contains the Observer interface and Observable class which allow objects to notify one another when they change. The java.util classes aren't covered separately in this tutorial although some examples use these classes.

Package
The Java Networking Package
The java.net package contains classes and interface definitions that implement various networking capabilities. The classes in this package include a class that implement a URL, a connection to a URL, a socket connection, and a datagram packet. You can use these classes to implement client-server applications and other networking communication applications.

Package
The Applet Package
This package contains the Applet class -- the class that you must subclass if you're writing an applet. Included in this package is the AudioClip interface which provides a very high level abstraction of audio.

Package
The Abstract Window Toolkit Packages
Three packages comprise the Abstract Window Toolkit: java.awt, java.awt.image, and java.awt.peer.
AWT Package
The java.awt package provides GUI elements used to get input from and display information to the user such as windows, buttons, scrollbars, and text items.
AWT Image Package
The java.awt.image package contains classes and interfaces for managing image data, such as setting the color model, cropping, color filtering, setting pixel values, and grabbing snapshots of the screen.
AWT Peer Package
The java.awt.peer package contains classes and interfaces that connect platform-independent AWT components to their platform-dependent implementation (such as Motif widgets or Microsoft Windows controls).

0 comments: