views:

258

answers:

6

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

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

+2  A: 

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/

George B.
+23  A: 

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

Jason
Your c++ example will fail to compile if you try to pass it a type that doesn't have addition defined. Same as your java example, with the method add. Your example does not illustrate template differences, but the lack of operator overloading in Java. Otherwise, good answer :)
gnud
@gnud: That's right, but there's a subtle difference. In C++ the template that I wrote above is legal without restriction on `T`; it's only if you try to use it with a type that doesn't have `+` defined will there be a failure. In Java, there is no equivalent. So in C++ you can say "define `sum` for anything that has `+`." In Java you can not say this; you can only specify that `T` extends `Something` that has an `add` method. Thus if you have a `U` that does not extend `Something` but it has an `add` method you can not use the above defined `sum` on instances of `U`.
Jason
C++ does sort of allow bounding of type parameters, but in a really roundabout way. Basically if a template expansion fails for certain reasons it is removed from the list of candidate expansions. This is called SFINAE (Substitution Failure Is Not An Error) in the C++ community. You can use this to do compile time introspection on classes and expanded certain things one way and other things another.
Omnifarious
C++ also allows non-type template parameters
sellibitze
@jason yes, in java you would have to provide an abstract interface, like Comparable
gnud
@Omnifarious: `boost::enable_if` FTW!
Jason
A: 

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.

Pace
+3  A: 

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".

Uri
The first paragraph is probably the most concise explanation of templates I've seen. +1 for that alone. :)
jalf
A: 

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.

fastcodejava
A: 

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.

Charles Eli Cheese
@charles wrote "...they generate much more code...". That's an over-generalization. Every language feature can be misused somehow. If you know what you're doing you won't have problems with code bloat. In addition, the compiled C++ code isn't clogged with the amount of meta data that is required in Java to be "more dynamic". But I agree with your first sentence. You can get pretty far by using runtime polymorphism + garbage collection. Performance will just suck a little.
sellibitze
well, the only time they won't generate more code is if you only have one type used by a template and only have one object file using the template. I don't mean to say never use templates since C++ sort of forces you to, but they won't have as much performance improvement over Java as most people think and also add a lot of complication.
Charles Eli Cheese