It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes again doubt in generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "again doubt in generic" Watch "again doubt in generic" New topic
Author

again doubt in generic

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
What is the main difference between <E> & <T> type. In book its written that <E> is used for Collection. <T> is used for things that are not collection. But I am not getting it. Where we can you <E> & where we can use <T>?

Please anybody help me.


SCJP-1.5<br />SCWCD-1.4
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

It doesn't matter which letter that you use to specify a generic type. It's just that once you use a letter to specify a type, using the same letter again, in the same context means that you're referring to the same type.

E & T are like variables only instead of representing primitives or objects, they represent types.

Kaydell
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

It is often seen as convention while naming parameterized type:
Letter E generally used with parameterized interfaces like:



Letter T is also used interchangeably.


Thanks,


cmbhatt
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Gilad Bracha wrotes in his generics tutorial that the naming convention is either <T> for type or (esp. in collections) <E> for element.

The tutorial (PDF) can be downloaded from Sun at:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf


Yours,
Bu.


all events occur in real time
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
In book its written that <E> is used for Collection. <T> is used for things that are not collection.

Wy they have written above sentence? If there is no difference, Why they have introduced (<E> & <T> ?
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks Chandra.
We can use any of them. Am I right?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Yes, you can also use something else than "E" or "T". It doesn't even have to be a single letter, you can use any name.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kantutan Tayo
Greenhorn

Joined: Oct 07, 2005
Posts: 22
It's just by code convention / suggestion.

E stands for Element (an element within a collection)
T stands for Type (a generic type)

It could have been a complete word instead of just a letter e.g.

interface Comaparable<Elephant> {
int compareTo(Elephant e);
}

But that's gay.


SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: again doubt in generic