Let's  Print Array Element

Java Generic

Why to use Generic

  • Java compiler guarantees type-safety and flag any type-safety relate error during compilation#
  • No casting
  • Reduce code

Generic methods and classes enables us to specify with a single method declaration a set of related methods and with a single  class declaration a set of related classes.

  • It was introduced in Java 1.5
  • strong type checking.
  • Eliminates explicit type checking
  • Makes algorithm much more reusable
  • compile-time type safety
  • the list already knows what types of objects are supposed to be acted on.
  • don't need to cast anything

        List list = new ArrayList(10);

list.add(“test”); 

list.add(10); 

list.add("java");

        Display (List list){

String str=(string) list.get(0);

String str=(string) list.get(1);// runtime error will come

String str=(string) list.get(2);


List <String> list = new ArrayList <string>();
list.add(“test”);
list.add(1); // compile time error will come here
list.add(“java”);
String str1= list.get(0);
String str1= list.get(1);
String str1= list.get(2);
   {    system.out.println(“astring)  }     

   or we could use the for each loop
     List <String> strings = new ArrayList <string>();
       for(String astring:list)
      {    system.out.println(“astring)  }                                          

Using Generic

So basically Generics forces the developer to add only specific objects to the collection to avoid runtime exception as shown in the example.

Generic Method

Generic Class

Subtyping

  • Integer is a subtype of Number
  • Double is a subtype of Number
  • ArrayList<E> is a subtype of List<E>
  • List<E> is a subtype of Collection<E>
  • Collection<E> is a subtype of Iterable<E>

Subtyping is a concept used for wildcards declaration.

Substitution Principle

  • a given type may be assigned a value of any subtypeof
  • Eg: Any element of type Number can accept Integer and Double values

Wildcards

 

  • Wildcards help in allowing more than one type of class in the Collections.
  • We come across setting an upperbound and lowerbound for the Types which can be allowed in the collection
  • The bounds are identified using a ? Operator  which means ‘an unknown type’

Upperbound

  • List<? extends Number>
    It means that the given list contains objects of some unknown type which extends the Number class

List<Integer> ints = new ArrayList<Integer>();

ints.add(2);

List<? Extends Number> nums = ints;  // Allowed because of wildcards

nums.add(3.14);  // This is not allowed now after setting an upperbound

Integer x=ints.get(1);

Lowerbound

  •  List<? super Number>
    It means that the given list contains objects of some unknown type which is superclass of the Number class

public static <T> void copy(List<? super T> dst, List<? extends T> src) {
for (int i = 0; i < src.size(); i++) {
dst.set(i, src.get(i));
}
}

Unbound

  • List<?>

Unbound

  • List<?>

When to use which wildcard?

The Get and Put Principle:

  • extends wildcard when you only get values out
  •  super wildcard when you only put values

Generic Interfaces

  • Comparable
  • Comparator

Comparable

interface Comparable<T> {
public int compareTo(T o);
}

Comparator

interface Comparator<T> {
public int compare(T o1, T o2);
public boolean equals(Object obj);
}

Some other Concepts

  • Reification
  • Reflections
  • Design Principles: eg:Iterator , 

Restrictions and limitations

  • in Java, generic types are compile-time entities

  • primitive type parameters (Pair <int>) not allowed

  • objects in JVM have non-generic classes:

Pair<String> strPair = new Pair<String> . .;
Pair<Number> numPair = new Pair<Number> . .;
b = strPair.getClass () == numPair.getClass ();
assert b == true;  // both of the raw class Pair

  • but byte-code has reflective info about generics
  • Generic Classes can not extend Throwable or any of its sub classes.

Thank you :D

Made with Slides.com