CSC/ECE 517 Fall 2011/ch6 6c sj: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 30: Line 30:
</code>
</code>
So here, <code>N</code> can be replaced with any primitive data types or wrapper classes in the business logic. But we need to make sure that placeholders to be replaced with valid subtypes of Object. Generics implementation is not restricted for classes or interfaces, we can have for static/non static methods and constructors.
So here, <code>N</code> can be replaced with any primitive data types or wrapper classes in the business logic. But we need to make sure that placeholders to be replaced with valid subtypes of Object. Generics implementation is not restricted for classes or interfaces, we can have for static/non static methods and constructors.
Example for generic methods:
<code>
static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
    for (T o : a) {
        c.add(o); // Correct
    }
}
</code>


==Reference==
==Reference==

Revision as of 22:35, 13 November 2011

What are Generics in Java?

Generics were added to Java programming language as a part of J2SE 5.0 release in 2004. They allow programmers to develop generic and compile safe applications which enables a type or method to operate on objects of various type. This feature is well utilized while implementing Java Collections. The reason why people went into generic programming can be well explained using the below example:

 List list = new ArrayList();
 list.add("name");
 Integer num = (Integer)list.get(0);

In this example, we insert a String into an ArrayList and retrieve element by typecasting with Integer wrapper class. This throws a runtime exception because of Illegal class cast. But if we use generics, runtime exceptions can be avoided and the compiler will be able to catch such issues which in turn help a programmer to build a bug free code. So if we convert the above code to use generic then it would look like this:

 List<String> list = new ArrayList<String>();
 list.add("name");
 Integer num = list.get(0); //line 1 

Here at line 1 the compiler throws an error because the compiler on seeing the declaration of ArrayList will be expecting the list to have just String and thereby the return type of get to be String.

Implementing Generics: Java provides a feature which helps one to implement your own generic types and this will help to build more sophisticated and runtime error free applications. Consider the below example:

  interface List<N> {
                        void add(N i);
                        Iterator<N> iterator();
                     }
  interface Iterator<N> {
                           N next();
                           boolean hasNext();
                        }
  class LinkedList<N> implements List<N> {
                         //Business Logic   
                }

So here, N can be replaced with any primitive data types or wrapper classes in the business logic. But we need to make sure that placeholders to be replaced with valid subtypes of Object. Generics implementation is not restricted for classes or interfaces, we can have for static/non static methods and constructors.

Example for generic methods:

static <T> void fromArrayToCollection(T[] a, Collection<T> c) {

   for (T o : a) {
       c.add(o); // Correct
   }

}

Reference

1. http://en.wikipedia.org/wiki/Generics_in_Java

2. http://www.oracle.com/technetwork/articles/javase/generics-136597.html

3. http://download.oracle.com/javase/tutorial/extra/generics/methods.html