什么是 C++ 模板的 Java 等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2159338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the Java equivalent of C++'s templates?
提问by giri
What is the Java equivalent of C++'s templates?
什么是 C++ 模板的 Java 等价物?
I know that there is an interface called Template. Is that related?
我知道有一个叫做 Template 的接口。那有关系吗?
回答by jason
Templatesas in C++ do not exist in Java. The best approximation is generics.
One huge difference is that in C++ this is legal:
一个巨大的区别是在 C++ 中这是合法的:
<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
Java 中没有等效的构造。你能说的最好的是
<T extends Something> T Sum(T a, T b) { return a.add(b); }
where Somethinghas a method called add.
whereSomething有一个方法叫做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
在 C++ 中,编译器会为代码中使用的模板的所有实例创建模板的编译版本。因此如果我们有
int intResult = sum(5, 4);
double doubleResult = sum(5.0, 4.0);
then the C++ compiler will compile a version of sumfor intand a version of sumfor double.
然后 C++ 编译器将编译一个版本的sumforint和一个版本的sumfor 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.
在 Java 中,有擦除的概念。发生的情况是编译器删除了对泛型类型参数的所有引用。编译器只创建代码的一个编译版本,而不管它与不同类型参数一起使用多少次。
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 defaultswhere Java does not
- C++ allows template specializationwhereas Java does not And, as should be expected by this point, C++ style template metaprogrammingis impossible with Java generics.
- Forget about seeing the curiously recurring template patternin Java
- Policy-based designis impossible in Java
回答by Uri
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.
Java 中没有真正的模板。C++ 模板是用于生成类的编译时实体。在运行时,没有它们的踪迹。
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.
在 Java 中,参数化类型是泛型机制的一部分。它具有类似的目的,但在其运作方式及其影响方面却截然不同。它在运行时有一些表示,有特定的规则等。
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".
首先阅读Java 教程,然后阅读 Bloch 的 Effective Java 以了解如果您想成为“高级用户”的注意事项的详细说明。
回答by George
There are no templates in Java. The only thing that is comparable with templates are Java Generics.
Java 中没有模板。唯一可以与模板相比的是 Java 泛型。
http://java.sun.com/developer/technicalArticles/J2SE/generics/
http://java.sun.com/developer/technicalArticles/J2SE/generics/
回答by Pace
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.
Java 具有与模板相似但不完全相同的泛型。我不知道 Template 接口是什么,但它与 C++ 模板无关。
回答by fastcodejava
回答by Charles Eli Cheese
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.
您实际上并不需要 Java 中的模板。当您需要将完全不同的类型作为您一无所知的参数时,模板主要很有用。在 Java 中,所有对象都是虚拟的并从一个根对象继承,并且原始类型的定义更清晰,强制转换以更合理的方式工作,因此实际上没有任何意义。
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.
您可以使用 C++ 样式模板获得更好的性能,但是由于它们生成更多代码的方式以及快速的 CPU 速度每年越来越多地超过数据访问速度,这种情况越来越少,尤其是在对象方面到原始人。

