Results 1 to 6 of 6
  1. #1
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default Generics in java

    Hi,
    I recently started looking into generics (a feature from java 5 onwards). Everything was fine until I somehow came across the Enumeration interface source code:

    package java.util;

    public interface Enumeration<E> {

    boolean hasMoreElements();

    E nextElement();
    }

    What is the 'E' over here ? Any ideas ?

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    'E' represents the generic type.

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by ajeeb View Post
    What is the 'E' over here ? Any ideas ?
    Its a dummy variable for the type, in other classes its a T or K or V.

    Enumeration<String> e = ...

    is how you get type safety of a String.

    Sun has some very nice tutorials on this stuff, you should take a quick read.

  4. #4
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    I got the idea. Its indeed a dummy variable stating that the Enumeration will contain objects of type E. Thanks to all.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    P.S. The developers chose the letter (normally) depending on what they wanted to "say". E normally stands for Element, T Type, K Key, and V Value. Not that that really makes any difference. ;-)

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Just a note: The generic type names don't have to be a single, uppercase letter. They can just as easily be Element, Key, Value, AnythingElseThatMakesSense. When I started using them in my own classes, I didn't know what letters I could use...

Similar Threads

  1. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 02:37 AM
  2. Java confused over Generics?
    By Bibendum in forum New To Java
    Replies: 3
    Last Post: 12-26-2007, 06:23 AM
  3. Java Generics (an introduction)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 11-27-2007, 06:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •