Monday, 6 October 2008

Beginning Java 2, JDK 5 Edition By Ivor Horton - Part5

Couple of Days back i started reading Beginning Java 2, JDK 5 Edition By Ivor Horton.

Nice book written by Ivor Horton.

Below are few quotations i found from this book from the next 2 chapters (13-14).

1) A generic type, which is also referred to as a parameterized type, is a class or interface type definition that has one or more type parameters. You define an actual class or interface type from a generic type by supplying a type argument for each of the type parameters that the generic type has.

2) A definition of a generic class type looks very much like the definition of an ordinary class, but with a parameter specification added following the class name. Here’s how the LinkedList class looks as an outline of a generic type:


public class LinkedList<T> { // T is the type parameter
// Generic type definition...
}


The parameter that appears between the angled brackets, <>, that follows the generic type name,LinkedList, is called a type parameter.

Occurrences of the type parameter name in the definition of a generic type are called type variables because they will be replaced by a value that is a type, in a similar way to how method parameters are replaced by the arguments that we supply.

3) Type Parameter Bounds

In some situations we will be defining a generic type where you want to constrain the type arguments that are supplied to define a class instance so that they extend a particular class, or implement specific interfaces, or even both. The reason for this would be that your generic type has to make some assumptions about the capabilities of the objects an instance of the type will be dealing with. Such constraints are called type parameter bounds.

To understand how you specify a type parameter bound, consider a simple example of where this applies.Suppose that you wanted to modify the LinkedList generic type so that objects of classes produced by this type would be serializable. Not only would LinkedList need to implement the Serializable interface, but objects of type T that were stored in the list would, too. The definition for the generic type would therefore look like this:


import java.io.Serializable;

class LinkedList<T extends Serializable> implements Serializable {
// Default constructor - creates an empty list
public LinkedList() {}

// Constructor to create a list containing one object
public LinkedList(T item) {
if(item != null) {
current=end=start=new ListItem(item); // item is the start and end
}
}

// Construct a linked list from an array of objects
public LinkedList(T[] items) {
if(items != null) {
// Add the items to the list
for(int i = 0; i < items.length; i++) {
addItem(items[i]);
}
current = start;
}
}

// Add an item object to the list
public void addItem(T item) {
ListItem newEnd = new ListItem(item); // Create a new ListItem
if(start == null) { // Is the list empty?
start = end = newEnd; // Yes, so new element is start and end
} else { // No, so append new element
end.next = newEnd; // Set next variable for old end
end = newEnd; // Store new item as end
}
}
// Get the first object in the list
public T getFirst() {
current = start;
return start == null ? null : start.item;
}

// Get the next object in the list
public T getNext() {
if(current != null) {
current = current.next; // Get the reference to the next item
}
return current == null ? null : current.item;
}

private ListItem start = null; // First ListItem in the list
private ListItem end = null; // Last ListItem in the list
private ListItem current = null; // The current item for iterating

private class ListItem implements Serializable {

// Constructor
public ListItem(T item) {
this.item = item; // Store the item
next = null; // Set next as end point
}

// Return class name & object
public String toString() {
return "ListItem " + item ;
}

ListItem next; // Refers to next item in the list
T item; // The item for this ListItem
}
}




4) The class that corresponds to the raw type is produced by removing the type parameters from the generic
type definition and replacing each instance of a type variable in the definition by the leftmost bound of its
correspond type parameter. This process of mapping from a generic type to a non-generic type is called
type erasure because all occurrences of the type variable are effectively erased from the generic class definition. A raw type exists as a consequence of implementing generic types using type erasure.

5)A generic type, which is also referred to as a parameterized type, defines a family of classes or interfaces using one or more type parameters. Container classes are typically defined as generic types.

6) The argument you supply for a type parameter can be a class type or an interface type. It cannot be a primitive type.

7) You can limit the scope of type arguments for a given type parameter by specifying one or more bounds for the parameter using the extends keyword. The first bound can be a class or interface type; the second and subsequent bounds must be interface types.

8) You define a specific type from a generic type by supplying a type argument for each type parameter.

9) All types produced from a given generic type share the same run-time type.

10) A parameterized method defines a family of methods using one or more independent type parameters.

11) A parameterized method can be a member of an ordinary class type or a generic type.

12) You can use wildcards as type arguments in a parameterized type in situations where there is no dependency on a specific type.

13) You can constrain a wildcard type argument with either an upper bound that you specify using the extends keyword or with a lower bound that you specify using the super keyword.

14) The Java collections framework provides you with a range of collection classes implemented as generic types. These enable you to organize your data in various ways.

15) You can use a Vector<> object as a kind of flexible array that expands automatically to accommodate
any number of objects stored.

16) The Stack<> class is derived from the Vector class and implements a pushdown stack.

17) The HashMap<> class defines a hash map in which objects are stored based on an associated key.

18) An Iterator<> is an interface for retrieving objects from a collection sequentially. An Iterator<> object allows you to access all the objects it contains serially—but only once. There’s no way to go back to the beginning.

19) The ListIterator<> interface provides methods for traversing the objects in a collection backwards or forwards.

20) Objects stored in any type of collection can be accessed using Iterator<> objects.

21) Objects stored in a Vector<>, a Stack<>, or a LinkedList<> can be accessed using ListIterator<> objects.

About the Author

Ivor Horton started out as a mathematician, but shortly after graduating, he was lured into messing about with computers by a well-known manufacturer. He has spent many happy years programming occasionally useful applications in a variety of languages as well as teaching mainly scientists and engineers to do likewise. He has extensive experience in applying computers to problems in engineering design and to manufacturing operations in a wide range of industries. He is the author of a number of tutorial books on programming in C, C++, and Java. When not writing programming books or providing advice to others, he leads a life of leisure.

No comments: