JDK 1.5 introduces many important extensions to the Java programming,Generics are one among them. generics are simple variables to types (which will bound at compile time) also called parametrized types. In a object oriented language,there is always a need for ‘generic’ types,for which the actual type of variable known only at run time. One of the simple example is java.util.List , it can contain any type of objects . So we need a casting to return objects from this list. Generics can apply both class and method level. generics can also be nested.
Generics make the code easy to read. consider method public List getList() ,it is not possible to know the type of list without documentation . But the use of public List will clearly express that the returned List is of type String
Generics enforce type safety also by reducing the need to cast references back to the actual types of the object, since using method add(“hai All”) on a List will produces a compile error but in non-generic code, this would not produce an error which may cause abnormal behavior in your system ,so the frequency of ClassCastExceptions should be reduced as the compiler catch type errors.
// without generics.
List emp = new ArrayList();
emp.add(“A”);
emp.add(“B”);

emp.add(“C”);
Iterator it = emp.iterator();
while (it.hasNext()) {
String empname = (String)it.next();

}

// Same using generics.

List
emp = new ArrayList();

emp.add(“A”);

emp.add(“B”);

emp.add(“C”);
Iterator
it = emp.iterator();
while (it.hasNext())
{

String empname = it.next();
}

Syntax of Generics
The declarations are done using angled brackets with comma-separated list of type parameters.
public class A<T>
{

public T get() { … }
}

( Any name can be used for parameter ,usually T,U,V.. series are taken and often certain type of alphabets are used to convey certain meanings like K – type of a key, E – element type etc )
Generics Wildcards
Generics wild cards are used when we really don’t know the parameter type to pass . Consider the situation ,Collection<Object> c = new ArrayList<String>
() will not compile because c.add(new Integer(1)) is perfectly legal but obviously an Integer is not a String. The data type which is unknown at compile time is represented by ? Wild card. Collection will be super class of all collections. Here ? behaves like replacer for any class type.
public void printAllCollection(Collection obj) {

for (Object o : obj)
{

System.out.println(o);
}
}

There are bounded and unbounded wildcards .There may be situations where we want to restrict the kinds of types that are used to pass as type parameter. For example,if a method that operates on numbers only need accept instances of Number or its subclasses.To declare parameter as bounded type , list the type parameter’s name, followed by the extends keyword, and then upper bound.
  • ? extend DataType-Denotes a family of subtypes of type DataType.
  • ? super DataType- Denotes all super class of type DataType
  • ? ?- Denotes the set of all types or any
  • Generics are implemented only at the language level. This concept is not carried down to the byte code level.
    Erasure
    The Generics is only language level feature,Your program is striped out of all parametric types, and each reference of parametric type is replaced with a class (often Object or more specific)on compile time. This process is known as type erasure.
    The Generics provides effective abstract and compile-time type safety in programming and eliminates the unsafe casting.