Monday 10 March 2014

Benefits Of Generics

Just like parameters to a method, Generics supports to the type parameters to the classes and Interfaces.

Type parameters provide a way for the programmer to re-use the same code with different inputs. The input to the parameters of a method is values, where as the input to the type parameters are types passed at the time of defining an object.

Benefits
1. Stronger type checks at compile time
Types are checked at compile time, so most of the type related bugs detected at compile time.
import java.util.*;
class TypeCheck{
 public static void main(String args[]){
  List<Integer> intList = new ArrayList<Integer> ();
  intList.add(10);
  intList.add("Hi");
 }
}
   
In the above program, intList is the object of ArrayList class, which accept only integers. But the statement 'intList.add("Hi")' tries to add a string to the intList. Which is not acceptable. So when you tries to compile the above program, compiler throws the below error.

TypeCheck.java:6: error: no suitable method found for add(String)
intList.add("Hi");
^
method List.add(int,Integer) is not applicable
(actual and formal argument lists differ in length)
method List.add(Integer) is not applicable
(actual argument String cannot be converted to Integer by method invocation conversion)
1 error

2. Generics eliminates the external casts
Example 1 : Program without Generics
import java.util.*;
class CastElimination{
 public static void main(String args[]){
  List intList = new ArrayList();
  intList.add(10);
  int var = (Integer)intList.get(0);
  System.out.println("Value of the variable is " +var);
 }
}

Output
Value of the variable is 10

As you observe the above program, The value from the intList must cast explicitly like below (Integer)intList.get(0)
Generics avoids this kind of explicit casting. See the Below Program.

Example 2 : Program with Generics
import java.util.*;
class CastEliminationEx{
 public static void main(String args[]){
  List<Integer> intList = new ArrayList<Integer>();
  intList.add(10);
  int var = intList.get(0);
  System.out.println("Value of the variable is " +var);
 }
}
    
Output
Value of the variable is 10
    
3. Enable Programmers to implement Generic Data Structures.
Example
class Stack<T>{
 Object arr[];
 int top=-1;
 int size;

 Stack(int size){
  this.size=size;
  arr = new Object[size];
 }

 void push(T data){
  if( top < size-1){
   top++;
   arr[top] = data;
  }
  else{
   System.out.println("Stack is full");
  }
 }

 T pop(){
  if(top < 0 ){
   System.out.println("Stack is Empty");
   return null;
  }
  else{
   System.out.println("Element deleted is " + arr[top]);
   T val = (T) arr[top];
   top--;
   return val;
  }
 }

 public static void main(String args[]){
  Stack<String> stringStk = new Stack<String> (5);
  Stack<Integer> intStk = new Stack<Integer> (5);

  stringStk.push("HI");
  stringStk.push("abc");
  stringStk.pop();

  intStk.push(10);
  intStk.push(20);
  intStk.pop();
 }
}

Output
Element deleted is abc
Element deleted is 20

As you observe the above program, stack is defines as generic type, stringStk is stack object reference variable of type string, where as intStk is of type Integer. So Stack class can be used to store any type of data. No need to write two stack programs, one for Integer, one for String, with the use of Generics.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment