Tuesday 18 March 2014

Erasure of Generic Methods

Just like Erasing the Types for classes Java Compiler erases the types for Methods also.

class Search{
 
 <T> boolean searchItem(T[] data,T item){
  for(T obj: data){
   if(item.equals(obj))
    return true;
  }
  return false;
 }
}
After performing the Type Erasure the code looks like below. Since type parameter “T” has no bounds, so T is replaced with Object.

class Search{
 
 boolean searchItem(Object[] data,Object item){
  for(Object obj: data){
   if(item.equals(obj))
    return true;
  }
  return false;
 }
}


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment