import java.util.ArrayList; public class UsageExample { public static void main( String args[] ) { // example without generics ArrayList numList = new ArrayList(); numList.add(new Integer(1)); numList.add(new Integer(2)); // no type checking, we can put strings into this list numList.add(new String("bad idea") ); // can't do this: get returns an Object // Integer i1 = numList.get(0); // Integer i2 = numList.get(1); // need to cast the returned Object reference to Integer Integer i1 = (Integer) numList.get(0); Integer i2 = (Integer) numList.get(1); // this line would cause the program to crash: // Integer i3 = (Integer) numList.get(2); System.out.println( i1.intValue() + i2.intValue() ); // now, with generics: // between the < and > characters is the generic type parameter ArrayList numListGeneric = new ArrayList(); numListGeneric.add(new Integer(1)); numListGeneric.add(2); // numListGeneric.add(new String("bad idea")); // this will not compile i1 = numListGeneric.get(0); // no casting needed i2 = numListGeneric.get(1); System.out.println( i1.intValue() + i2.intValue() ); } }