Generics       P. Komisar       
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html


Generics were introduced in JDK 1.5.

"This long-awaited enhancement to the type system allows a
type or method to operate on objects of various types while
providing compile-time type safety. It adds compile-time type
safety to the Collections Framework and eliminates the
drudgery of casting" .

"When you take an element out of a Collection, you must cast
it to the type of element that is stored in the collection. Besides
being inconvenient, this is unsafe. The compiler does not check
that your cast is the same as the collection's type, so the cast
can fail at run time. "

"Generics provides a way for you to communicate the type of a
collection to the compiler, so that it can be checked. Once the
compiler knows the element type of the collection, the compiler
can check that you have used the collection consistently and can
insert the correct casts on values being taken out of the collection."

                                         - Quote from JDK 1.5 Documentation

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

Following is a simple example to show the salient points of how
generics are used. Storing String objects was not the best choice
because objects have a default toString( ) method which could
confuse the issue. Still, the behaviour is well reported by the
compiler with respect to using Vector's add( ) and elementAt( )
methods. Try compiling in the JDK 1.5 environment without the
two <String> phrases. Then add them back in and recompile.
(May also need to add the (String) cast to the elementAt( ) call.)


Generics Provide Strong Typing of a Vector to String Type at Compile Time


import java.util.Vector;

class Generics{
   
// new --> 'generics' --> ensures strong typing
// eliminates illegal casting runtime errors
// For varied type could have used <Object>

// test by removing the <String> phrases
// and see the compiler report
  
    Vector<String> notes;
    
   public Generics(){
     super();
     notes = new Vector<String>();
     notes.add("Morning Note");
     notes.add("Afternoon Note");
     notes.add("Evening Note");
   
     }
    
 String getMorningNote(){
        String report =notes.elementAt(0) ;
     // normally would do a cast here
     // (String)notes.elementAt(0);
     
        return report;
        }
      
      // generics makes casting unneccessary
      // though if set to <Object> the need
      // to cast may return
    
      public static void main(String[]args){
      Generics generics = new Generics();
      System.out.println(generics.getMorningNote());
      }
      }