Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does java support templates?

 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all the time I used Java I thought it didnt then I ran across this:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashSet.html

can any boy elaborate.

Thanks
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 1.5 - currently in beta - introduces templates ("generics") into the language. If you're familar with C++, beware: they are reminiscent of C++ templates and are superficially similar, but they work quite differently under the hood.

- Peter
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
Java 1.5 - currently in beta - introduces templates ("generics") into the language. If you're familar with C++, beware: they are reminiscent of C++ templates and are superficially similar, but they work quite differently under the hood.


In fact, Java does *not* use templates to implement generics.

C++ templates use a compile-time model. When a template is used in a C++ program, the effect is as if a sophisticated macro processor had been used.

Java generics (as well as C# generics) are not just a feature of the compiler, but also a feature of the runtime. A generic type such as List<T> maintains its generic-ness (genericity) after it has been compiled.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
In fact, Java does *not* use templates to implement generics.
That's what I was trying to warn about. Semantics, you've gotta love 'em Thanks

- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic