Generics Introduction:
- Generics are parameterized types, where class, interface methods are parameterized with types.
- Same like the more parameters used in method declarations, type parameters provide a way for you to re-use the same code with different input types.
- The difference is that method receives values as arguments and generics receive types as an argument.
- Advantage: Strong type checks at compile time or type safety.
The following code is without generics which require type casting:
List l = new ArrayList();
l.add("java");
String s = (String)l.get(0);
When re-written to use generics, the code does not require casting:
List<String> l = new ArrayList<String>();
l.add("java");
String s = l.get(0); // no typecasting
Generic Types: A generic type can be applied to,
- Generic Class
- Generic Interface
- Generic Method
Generic Class: A class can be parameterized with type. This will receive data type as an argument.
public class Container
{
private Object obj;
public void set(Object obj)
{
this.obj = obj;
}
public Object get()
{
return obj;
}
}
Methods of Container class accept and return Object; you can pass any type you want.
Container c=new Container();
c.set(new Integer(10))
Integer x=c.get();
If some part of the code store an Integer in the Container and allows to get Integers out of it, while another part of the code may mistakenly set a String, which result to runtime error.
class name<T1,T2,T2,...>{
………
}
Type parameters are defined within angle brackets <> , which follows the class name. Here T1,T2,T3 are type parameter names.
public class Container<T> {
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}
Generic parameter name is a single letter in uppercase. Commonly used parameters names.
- E – Element Type ( used in collection framework)
- K – Key Type(used in collection famework)
- N – Number
- T – Type (class,interface types)
- V – Value (Value types)
Invoking and Instantiating a Generic Type: To reference the generic Container class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer:
Container<Integer>integerContainer;
To create object of generic class, use the new keyword, but place <Integer> between the class name and the parenthesis:
Container<Integer>integerContainer = new Container<Integer>();
publicclass Container<T> {
private T t;
void set(T t) {
this.t = t;
}
T get() {
returnt;
}
publicstaticvoid main(String args[]) {
Container<Integer>c1 = new Container<Integer>();
c1.set(new Integer(10));
System.out.println(c1.get());
Container<String>c2 = new Container<String>();
c2.set(new String("Java"));
System.out.println(c2.get());
}
}
Generic Methods: Generic methods are methods that are parameterized with type parameters. Theses parameterized types are local to that method. You can declare generic methods (static or non static) or constructors.
Syntax:
public static <T>dataTypemethodName(GenericsType<T> g1, GenericsType<T> g2){ }
Code:
class GenericMethodTest
{
static<T> void printObject(T obj)
{
System.out.println(obj);
}
public static void main(String args[])
{
printObject(new Integer(10));
printObject(new String(“Java”));
}
}