Nice book written by John Zukowski.
Few important quotations i wanted to share from the remaining chapters.
1) The Set Interface
The Set interface is like the java.util.Set interface and defines an unordered collection of elements. Like java.util.Set, the JGL interface includes methods to add and remove elements and check for their existence.
Adding elements: put()
Removing elements: remove()
Checking existence: get() and count()
There are just two concrete sets found in the JGL Libraries: HashSet and OrderedSet.
The following example demonstrates using a HashSet:
m
import com.objectspace.jgl.*;
public class HashSetTest {
public static void main(String args[]) {
Set set = new HashSet();
for (int i=0, n=args.length; i<n; i++) {
set.put(args[i]);
}
System.out.println(set);
}
}
2) Copy−on−Write Collections
The copy−on−write implementations, CopyOnWriteArraySet and CopyOnWriteArrayList, allow you to maintain groups of immutable objects. When it is time for the group to change, they change en masse with no possible way to see the intermediate group or with only some of the elements changed. This is important when you are iterating through the set of objects, as it is impossible for the group of objects to be corrupted even if the collection simultaneously changes the items in the group.
There are three constructors for CopyOnWriteArrayList:
public CopyOnWriteArrayList()
public CopyOnWriteArrayList(Collection c)
public CopyOnWriteArrayList(Object[] arrayToCopyIn)
3) Generic types, parameterized types, or templates are essentially different names for the same thing. (Though some will argue that generic types are not templates.) They represent the ability to work with collections of objects in a type−safe manner where, instead of working with a collection of Object instances, you work with a collection of a specific type. They are not presently a part of the Java programming language. At the time Java was introduced back in 1995, James Gosling, the father of Java, was strongly interested in keeping things simple and getting things right. The concept of templates was not simple and sufficient resources couldn't be spent on getting things designed properly, thus templates were left out of the initial release of Java.
4) Generic Type Basics
The basic concept of working with generic types is that when you define a class, you specify where the generic type fits in. For instance, the following defines a Vector of some type T, the type parameter:
Vector<T>
When the class is later used, the actual type is specified, either in a variable declaration:
Vector<String> stringVector;
. . .or in calling its constructor:
new Vector<String>()
Now, whenever you add elements to the collection or get items out, the only datatype that works is that which was passed to the template, in this case, String. If, when using the object, you pass in an incompatible datatype, you'll get a compile−type error, instead of waiting for a runtime exception when you try to cast to an improper type.
5) Getting generic types to work without changing the Java Virtual Machine (JVM) and with nongeneric libraries requires a bit of thought, as do issues like support for primitive types and dealing with run−time type data (for instance, with casting and instanceof). Besides just the syntax, these are the issues that the working group is actually working on.
6) Generic Java
Generic Java (GJ) is one such implementation for adding parameterized−type support into Java. It relies on a preprocessor to work and provides a custom set of parameterized core library classes that need to be properly installed into the bootclasspath before the system libraries in order to run. GJ was designed by Gilad Bracha of Sun Microsystems, Martin Odersky of the University of South Australia, David Stoutamire of Sun
Microsystems, and Philip Wadler of Bell Labs, Lucent Technologies. The following demonstrates how to create and use classes with GJ.
7) Defining Generic Classes and Interfaces
Classes and interfaces designed to support templates are defined by specifying the type parameter with the class definition. GJ uses angle brackets to surround the type parameter, as in A element. For example, the following demonstrates the creation of a LinkedList class that implements a Collection interface, which supports an Iterator for iterating through the elements:
interface Collection<A> {
public void add(A x);
public Iterator<A> iterator();
}
interface Iterator<A> {
public A next();
public boolean hasNext();
}
class NoSuchElementException extends RuntimeException {}
8) JSR 14
The Java Specification Request for generic types is still in committee. How it appears once it gets out and if it will ever be added to the language are still essentially up in the air. If you are interested in following the JSR that can extend the Java programming language to support generic types, you may want to watch the following sites:
http://java.sun.com/aboutJava/communityprocess/jsr/jsr_014_gener.html
Java Community Process homepage for JSR #000014: "Add Generic Types to the Java Programming Language."
http://java.sun.com/people/gbracha/generics−update.html
Gilad Bracha is the JSR specification head. He maintains this resource, which provides information on the specific JSR members, a not−so−current status of the JSR, and a link to a JavaOne presentation on adding genericity.
About the Author
John Zukowski has been involved with the Java platform since it was just called Java, 11 years and running, since 1995. He is actively working with SavaJe Technologies to finish up the JavaOne 2006 device of show: the Jasper S20 mobile phone. He currently writes a monthly column for Sun’s Core Java Technologies Tech Tips and Technology Fundamentals Newsletter. He has contributed content to numerous other sites, including jGuru, DevX, Intel, and JavaWorld. He has written many other popular titles on Java, including Java AWT Reference (O’Reilly), Mastering Java 2 (Sybex), Borlands’ JBuilder: No Experience Required (Sybex), Learn Java with JBuilder 6 (Apress), Java Collections (Apress), and The Definitive Guide to Swing (Apress).
No comments:
Post a Comment