Why Generics in java

  • Generics was introduced in java 1.5
  • Generics enable classes and interfaces to be parameters while defining it, these parameters are type parameters.
  • Type parameters helps to provide a way to reuse the same code but with different input argument types.
  • The reason why Generics came into picture is to reduce the runtime exceptions in java programs, since before java 1.4 collections does not use to take parameters and developers where not sure which type of object is stored in that collection, developer always had take the object from the collection and cast it and check it, and if the object does not cast properly then it would result into a run time exception. To prevent it from happening using generics we can define what kind of object is stored in the collection while declaring it itself and could prevent the effort of casting the object and possible run time exception while code execution.
  • Thus by using generics we could prevent a lot of run time exceptions by declaring the type along with collection declaration.
  • We need not worry about casting the object since JVM already knows what kind of object is stored in the collection.
  • We can implement sorting searching algorithm in a very generic way using the generics functionality.

Advantages of Generics over non generic code:

  1. Stronger type check at compile time
    If violated then it results into a compile time error.
  2. We can eliminate casting
    List nameList = new ArrayList();
    String name = (String)nameList.get(0);

    List<String> nameList = new ArrayList<>();
    String name = nameList.get(0);
  3. Generic algorithms can be implemented to reuse search and sorting algorithms with the same code.

How to write a Generic Class?

  • Generic is identified by <T>, it is not necessary to be T but any alphabets. We take T commonly in the examples to suggest T as Types
  • If a Data Member(not member function) is a class uses a Generic Data member then its class name show be followed by name of the generic types inside an diamond operator.

Example :

class GenericClass<A,B,C>{
	A varA;
	B varB;
	C varC;
}

Example : On how a single classes variable can store different datatype

package com.test;

public class Demo {
	public static void main(String[] args) {
		Test<String> stringTest = new Test<>();
		stringTest.setItem("Hello");
		System.out.println(stringTest);
		Test<Integer> intTest = new Test<>();
		intTest.setItem(50);
		System.out.println(intTest);
		Test<Double> doubleTest = new Test<>();
		doubleTest.setItem(50.55);
		System.out.println(doubleTest);
		
	}
}
class Test<T>{
	T item;

	public T getItem() {
		return item;
	}

	public void setItem(T item) {
		this.item = item;
	}
	@Override
	public String toString() {
		return this.item.toString();
	}
}

Example : Multiple Parameters

package com.test;

public class DemoHashTable {
	public static void main(String[] args) {
		Hashtable<String,Integer> h1 = new Hashtable<>("Ten",10);
		System.out.println(h1);
		
		Hashtable<String,String> h2 = new Hashtable<>("Ten","Ten");
		System.out.println(h2);
	}
}
class Hashtable<K,V>{
	private K k;
	private V v;
	Hashtable(K k,V v){
		this.k = k;
		this.v = v;
	}
	@Override
	public String toString(){
		return k.toString()+" - "+v.toString();
	}
}

Common Characters Used in Generics:

  • K – Key
  • E – Element
  • N – Number
  • T – Type
  • V – Value
  • S,U,V – Multiple Type

Leave a Comment