Showing posts with label Generics. Show all posts
Showing posts with label Generics. Show all posts

Generics in Java


Generics are introduced in JDK 1.5 on-wards and one of the important and flexible feature in Java by enabling types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Its same as like other formal parameters by providing way of reusing code for different input types. Only the difference in inputs to formal parameters are values where as for inputs to type parameters are types.  
Generics in java

For developers to pass type or method to operate on objects of various types. Also it provides compile time type safety by making fully statically typed language. There are lot of advantage on Generics compared to non-Generic codes like,

  • Use of Multiple Bounds
  • Stronger type checks at compile time.
  • Elimination of casts.
  • Use of wildcards with extends or super to increase API flexibility
  • Enabling programmers to implement generic algorithms.

Now lets see few examples on using Generics in Java.


public class GenericsTest {

 public static void main(String[] args) {
  
  // Without Genric
  List list = new ArrayList();
  list.add("Hello Java Discover");
  // Here we need casting while we use non-generic code
  String val = (String) list.get(0);
  
  
  // With Genric
  List<String> list1 = new ArrayList<String>();
  list1.add("Hello Java Discover");
  // No need of casting while we use Generic
  String val1 = list1.get(0);  
 }
}


If we see in above we declared 2 List object without Generic type and 2nd with "String" type setting. When we get values from 1st List specifically we need to cast those values to corresponding type. But while we get values from 2nd list no need to casting to "String" as like 1st List. 


Next we write single method which will take multiple type inputs like integer, float, String etc.,


public class GenericsTest {

 public static void main(String args[]) {
  
  Integer[] iArray = new Integer[] { 10, 30, 50, 20, 60, 70 };
  System.out.print("Integer Array : ");
  genericMethod(iArray);
  
  Float[] fArray = new Float[]{ 4.41f, 5.23f, 2.23f, 99.41f };
  System.out.print("Float Array   : ");
  genericMethod(fArray);
  
  String[] sArray = new String[]{ "Hello", "Java", "Discover", "2013"};
  System.out.print("String Array  : ");
  genericMethod(sArray);  
 }

 public static <E> void genericMethod(E[] array) {
  for (E element : array) {
   System.out.print(element+", ");
  }
  System.out.println();
 }
}

OUTPUT:


Integer Array : 10, 30, 50, 20, 60, 70, 
Float Array   : 4.41, 5.23, 2.23, 99.41, 
String Array  : Hello, Java, Discover, 2013, 

In above program we have used genericMethod() function to print all 3 different types like integer, float and string. 

Next we will see about creating Generic class using Types.


public class MyGenericClass<T> {

 private T t;

 public MyGenericClass(T t) {
  this.t = t;
 }

 public T getValue() {
  return t;
 }
}


public class GenericsTest {

 public static void main(String args[]) {

  MyGenericClass<Integer> iValue = new MyGenericClass<Integer>(100);
  MyGenericClass<String> sValue = new MyGenericClass<String>("Hello Java Discover");
  MyGenericClass<Double> dValue = new MyGenericClass<Double>(434.23);

  System.out.println("Integer Value : " + iValue.getValue());
  System.out.println("String Value  : " + sValue.getValue());
  System.out.println("Double Value  : " + dValue.getValue());
 }
}

OUTPUT:


Integer Value : 100
String Value  : Hello Java Discover
Double Value  : 434.23

In above example we have created Generic class called "MyGenericClass" which will take different type constructor parameters.