tags:

views:

209

answers:

3
+3  Q: 

Java Generics?

Over the years I seen many people use the word "generics", I honestly have not a clue what it means, whatever it is I most likely use it but just don't know that it was called that. :p

+19  A: 

From http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

Here is a simple example taken from the existing Collections tutorial:

    // Removes 4-letter words from c. Elements must be strings
    static void expurgate(Collection c) {
        for (Iterator i = c.iterator(); i.hasNext(); )
          if (((String) i.next()).length() == 4)
            i.remove();

}

Here is the same example modified to use generics:

    // Removes the 4-letter words from c
    static void expurgate(Collection<String> c) {
        for (Iterator<String> i = c.iterator(); i.hasNext(); )
          if (i.next().length() == 4)
            i.remove();

}

Sorry for the direct c&p but I found that this write up was better than something I could have written.

Edit to include a good point made in the comments:

Generics are not limited to communicating the type of a collection to the compiler...the collections library just happened to be a good way to demonstrate them.

instanceofTom
+1 for a complete and comprehensive answer with a link to the documentation.
Grant Wagner
I would also recommend reading the Generics chapter of Effective Java (2nd edition) -- available online at http://java.sun.com/docs/books/effective/generics.pdf. This is particularly useful if you are writing a generic class (instead of just using one as in the example).
Kathy Van Stone
@Kathy, +1 good link
instanceofTom
I think it could be worth noting that generics are not limited to "communicat[ing] the type of a collection to the compiler", but that the collections library just happened to be a good way to demonstrate them. Unfortunately FutureTask<V> is the only example I can think of at the moment. +1
Grundlefleck
@ Grundelfleck good point
instanceofTom
A: 

Generics are just Java's Implementation of Parametric Polymorphism. They work just like Parametric Polymorphism in any other language.

Jörg W Mittag
Not quite. In Java, generics are implemented via type erasure. In other words, they're just syntatic sugar and don't change the underlying bytecode at all. This is very different from generics in, e.g., C++ or C# where the parameterized types are actually different types.
Barry Wark
I was tempted to add "... only uglier and worse" to the last sentence, but I thought that would be little unfair. However, Java Generics were designed by Phil Wadler, among others, who was also involved in the design of Haskell, which probably has the most comprehensive implementation of Parametric Polymorphism.
Jörg W Mittag
+3  A: 

It basically boils down to a reduction in the number of times you have to cast. ;-)

List<String> lst = new ArrayList<String>();
...
String first = lst.get(0);

versus

List lst = new ArrayList();
...
String first = (String) lst.get(0);
Wilfred Springer