Showing posts with label what is generics with an example. Show all posts
Showing posts with label what is generics with an example. Show all posts

what is generics in java1.5 with an example?



Generics in Java is one of important feature added in Java 5 along with Enum, autoboxing and varargs , to provide compile time type-safety

Generics notations and naming conventions

Generic Term
Meaning
Set<E>
Generic Type , E is called formal parameter
Set<Integer>
Parametrized type , Integer is actual parameter here
<T extends Comparable>
Bounded type parameter
<T super Comparable>
Bounded type parameter
Set<?>
Unbounded wildcard
<? extends T>
Bounded wildcard type
<? Super T>
Bounded wildcards
Set
Raw type
<T extends Comparable<T>>
Recursive type bound


T – used to denote type
E – used to denote element
K – keys
V - values
N – for numbers
Exampl
 public class GenericVsArray
 {
      public static void main(String args[])
   {
      Holder<Integer> numbers = new Holder<Integer>(10);
      numbers.add(101);
      System.out.println("Get: " + numbers.get(0));
    }
  }