Generics in Java : An introduction

You are working with your team of developers and you have created a List to hold some values in it.
The other day a new team-member joins the team and starts working on the code.
He uses the List created by you to add items of different data-types (say string, long, Human etc.)
The code compiles sucessfuly, but will give a run-time error when you process the List at some point of time (e.g. sum-up all the numbers stored in the List).
Then there comes Generics to rescue you.

Lets see how as explained by Ms. Sindhu Kumari.

Generics are possibly the most talked about feature of Java 5. It sounds scary, but actually it is simply an enhancement to java programming language for compile-time type safety.
Lets understand the need for introducing generics.
Arrays in Java have always been type safe—an array declared as type String (string[]) can't accept Integers (or ints), Humans, or anything other than Strings. But what about the collection classes such as List, Map, Set, Vector and so on….

Observe this:
List myList = new ArrayList(); // can't declare a type
myList.add("Fred"); // OK, it will hold Strings
myList.add(new Human()); // and it will hold Humans too
myList.add(new Integer(42)); // and Integers...

A non-generic collection can hold any kind of object! A non-generic collection is quite happy to hold anything that is NOT a primitive.
This meant that all the care has to be taken by the programmer, because there was no way to guarantee the collection type. Usually, If we try to assign an int to a boolean reference or a String to a Horse reference, you get a compilation error by the java compiler, but with collections,the door is always open for all kind of objects.
And since a collection could hold anything, the methods that get objects out of the collection could have only one kind of return type—java.lang.Object. That meant that even for taking a String out of a list that is meant to hold String objects only, we need to do a cast :
String s = (String) myList.get(0);
And since you couldn't guarantee that what was coming out really was a String (since you were allowed to put anything in the list), the cast could fail at runtime, giving you a ClassCastException.
So, generics takes care of both ends (the putting in and getting out) by enforcing the type of your collections. Let's update the String list:

List myList = new ArrayList();
myList.add("Fred"); // OK, it will hold Strings
myList.add(new Human()); // compiler error!!


Perfect. That's exactly what we want. By using generics syntax—which means putting the type in angle brackets , we're telling the compiler that this collection can hold only String objects. The type in angle brackets is referred to as either the "parameterized type," "type parameter," or of course just old-fashioned "type."
So, now that what you put IN is guaranteed, you can also guarantee what comes OUT, and that means you can get rid of the cast when you get something from the collection. Instead of
String s = (String)myList.get(0); // pre-generics, when a
// String wasn't guaranteed
we can now just say
String s = myList.get(0);


And of course you can declare a type parameter for a method argument, which then makes the argument a type safe reference:
void takeListOfstrings(List strings) {
strings.add("foo"); // no problem adding a String
}


Return types can obviously be declared type safe as well:
public Set getStudentList()
Set students = new HashSet();
// more code to insert students
return students;
}

Conclusion:
Among all the features introduced in Java 5, generics is the most powerful one. Java developers need a little time to get used to generics. But when they start using it, they surely will save time and efforts...by writing easier and more robust code.

References :

3 comments:

sowmya said...

sowmya said...
Its really gud n informative.Actually i didnt have any knowledge abt it but now i have some idea abt it.The language used is also simple so its easy to understand.I really liked it and i will be visiting it daily

sowmya said...

sowmya said...
Its really gud n informative.Actually i didnt have any knowledge abt it but now i have some idea abt it.The language used is also simple so its easy to understand.I really liked it and i will be visiting it daily

Sindhu Kumari said...

Thank you Sowmya for your valuable comment..

We will keep this good work in future.. and will be posting
more articles on this topic....

Thanks. Keep visiting.