• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 5 Generics  RSS feed

 
Srivatsan santhanam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I saw that in Java 5 we can make use of Generics to have declarations like

Set<String> s = new HashSet<String>();

but, Can i also use UserDefined Objects also ? like

Set<EmployeeName> s = new HastSet<EmployeeName>();

Does this Generics information also made part of the Genearated .class file (byteCode) ? or is it only meant for Java Compiler and is lost after compilation?


-Srivats
 
Joni Salonen
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can use user defined objects, as long as they fullfill the requirements of the container poses; e.g. HashSet requires you to implement the equals and hashCode methods or otherwise it won't work quite the way you might think.

The type information is lost when you compile. This is called "type erasure."
See also
http://java.sun.com/docs/books/tutorial/java/javaOO/generics.html
http://java.sun.com/developer/technicalArticles/J2SE/generics/index.html
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!