Templates in Java

Started by
8 comments, last by doctorsixstring 21 years, 8 months ago
Does Java support generic programing? Is there a way to implement C++ style templates in Java? -Mike
Advertisement
Nope, Java doesn''t have templates.
Java doesn''t, and never will, support anything even close to C++ templates. However, version 1.5, due for beta release in a year or so, will support something that looks kind of like C++ templates in syntax and use...

The implementation won''t be anything like what C++ uses, though. Specifically, it will just be a mechanism for improving clode clarity and compile-time type checking. It won''t have any of the other uses C++ templates have.

As for whether generic programming is possible in java: somewhat. It really depends on what you mean by generic programming. If you want to create a data structure that can hold any kind of object (but not primitives), that can be done. If you want to use any of templates'' other uses, you''ll need to find another way.
quote:Original post by c_wraith
Java doesn''t, and never will, support anything even close to C++ templates. However, version 1.5, due for beta release in a year or so, will support something that looks kind of like C++ templates in syntax and use...

The implementation won''t be anything like what C++ uses, though. Specifically, it will just be a mechanism for improving clode clarity and compile-time type checking. It won''t have any of the other uses C++ templates have.

As for whether generic programming is possible in java: somewhat. It really depends on what you mean by generic programming. If you want to create a data structure that can hold any kind of object (but not primitives), that can be done. If you want to use any of templates'' other uses, you''ll need to find another way.


I think you''re being a bit harsh on Java here. The original question was whether Java supports generic programming. The answer to that is it does in a limited fashion now (everything is an Object - you can always wrap primitives in an Object and they have Object equivalents anyway), and it will in a much improved form in a near future release. A lot of C++ template use is just for generic data containers anyway.
Harsh? Not really. I actually like java a lot. I rather like that the templates as proposed are so much weaker than those in C++. Their primary use will be making the language have stronger compile-time guarantees, which is a very useful change. But they really are nothing like templates in C++, and people who want C++ templates won''t be satisfied with java''s generics, or java now.
quote:Original post by Dobbs
A lot of C++ template use is just for generic data containers anyway.


I can''t resist to point out that templates are for more
than generic data containers. Althought that was the intent
when templates were added to C++, some very clever
people found use of templates to do compile-time programming,
or template metaprogramming. Pretty powerful stuff if you
ask me.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
quote:Original post by Dobbs
...Althought that was the intent
when templates were added to C++, some very clever
people found use of templates to do compile-time programming,
or template metaprogramming. Pretty powerful stuff if you
ask me...


Would you be so kind to share it with us?



My compiler generates one error message: "Doesn''t compile."
-Albert Tedja-
My compiler generates one error message: "does not compile."
quote:Original post by nicho_tedja
Would you be so kind to share it with us?


Far be it for me to explain, the two definitive
books on template metaprogramming are Modern C++ Design
and Generative Programming. There are also many
articles written on this subject since its
discovery. Search on CUJ...


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
There are already Java compilers that supports generics (the page also contains a link to a prototype compiler from Sun that implements the generics proposal that will be included in JDK1.5).

It is correct that Java's generics will not support uses like template metaprogramming, but that limitation is intentional and perfectly in line with Java's design philosophy as such techniques tend to kill readability.

[edited by - HenryAPe on July 31, 2002 6:28:54 AM]
quote:Original post by nicho_tedja
Would you be so kind to share it with us?
Here''s a compile-time fibonacci number calculator. The parameter to fib() needs to be a constant known at compile time.
  #define fib(x) (MUHAHA<x>::value)template <int x>struct MUHAHA {  static const int value = fib(x - 1) + fib(x - 2);};template <>struct MUHAHA<1> {  static const int value = 1;};template <>struct MUHAHA<2> {  static const int value = 1;};  

This topic is closed to new replies.

Advertisement