开发者

What is the Java equivalent of C++'s templates?

开发者 https://www.devze.com 2022-12-18 05:30 出处:网络
What i开发者_C百科s the Java equivalent of C++\'s templates? I know that there is an interface called Template. Is that related?Templates as in C++ do not exist in Java. The best approximation is gen

What i开发者_C百科s the Java equivalent of C++'s templates?

I know that there is an interface called Template. Is that related?


Templates as in C++ do not exist in Java. The best approximation is generics.

One huge difference is that in C++ this is legal:

<typename T> T sum(T a, T b) { return a + b; } 

There is no equivalent construct in Java. The best that you can say is

<T extends Something> T Sum(T a, T b) { return a.add(b); }

where Something has a method called add.

In C++, what happens is that the compiler creates a compiled version of the template for all instances of the template used in code. Thus if we have

int intResult = sum(5, 4);
double doubleResult = sum(5.0, 4.0);

then the C++ compiler will compile a version of sum for int and a version of sum for double.

In Java, there is the concept of erasure. What happens is that the compiler removes all references to the generic type parameters. The compiler creates only one compiled version of the code regardless of how many times it is used with different type parameters.

Other differences

  • C++ does not allow bounding of type parameters whereas Java does
  • C++ allows type parameters to be primitives whereas Java does not
  • C++ allows templates type parameters to have defaults where Java does not
  • C++ allows template specialization whereas Java does not And, as should be expected by this point, C++ style template metaprogramming is impossible with Java generics.
  • Forget about seeing the curiously recurring template pattern in Java
  • Policy-based design is impossible in Java


There are no real templates in Java. C++ templates are compile-time entities that are used to generate classes. At runtime, there is no trace of them.

In Java, there are parameterized types as part of a mechanism called generics. It serves a similar purpose, but is significantly different in how it operates and its implications. It has some representation at runtime, there are specific rules, etc.

Start by reading the Java tutorial, then read Bloch's Effective Java for a detailed description of the caveats if you want to be a "power user".


There are no templates in Java. The only thing that is comparable with templates are Java Generics.

http://java.sun.com/developer/technicalArticles/J2SE/generics/


Java has generics which are similar but not exactly the same as templates. I don't know what the Template interface is but it has nothing to do with C++ templates.


There is no build in template mechanism in java. In stead they have generics. Moreover, ides have something called code templates e.g. for eclipse.


You don't really need templates in Java. Templates are mainly useful when you need to have completely different types as a parameter that you know nothing about. In Java, all objects are virtual and inherit from one root object, and primitive types are defined much more clearly and casts work in a more sensical manner so there's really no point to it.

You can get better performance with C++ style templates, but due to the way they generate much more code and the fast CPU speed outstrips data access speed more and more every year this is less and less the case, especially when it comes to objects as opposed to primitives.

0

精彩评论

暂无评论...
验证码 换一张
取 消