175

Java has generics and C++ provides a very strong programming model with templates. So then, what is the difference between C++ and Java generics?

JesseTG
  • 2,025
  • 1
  • 24
  • 48
popopome
  • 12,250
  • 15
  • 44
  • 36

13 Answers13

170

There is a big difference between them. In C++ you don't have to specify a class or an interface for the generic type. That's why you can create truly generic functions and classes, with the caveat of a looser typing.

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

The method above adds two objects of the same type, and can be used for any type T that has the "+" operator available.

In Java you have to specify a type if you want to call methods on the objects passed, something like:

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

In C++ generic functions/classes can only be defined in headers, since the compiler generates different functions for different types (that it's invoked with). So the compilation is slower. In Java the compilation doesn't have a major penalty, but Java uses a technique called "erasure" where the generic type is erased at runtime, so at runtime Java is actually calling ...

Something sum(Something a, Something b) { return a.add ( b ); }

Nevertheless, Java's generics help with type-safety.

nbro
  • 15,395
  • 32
  • 113
  • 196
Alexandru Nedelcu
  • 8,061
  • 2
  • 34
  • 39
  • 29
    He is perfectly correct that it is just an elaborate syntactic sugar. – alphazero Aug 04 '11 at 23:56
  • 46
    It's not purely syntactic sugar. The compiler uses this information for checking types. Even though the information isn't available at runtime I wouldn't call something the compiled uses simply "syntactic sugar". If you would call it that, well then C is just syntactic sugar for assembly, and that's just syntactic sugar for machine code :) – dtech Feb 03 '13 at 22:17
  • 52
    I think *syntactic sugar* is useful. – poitroae Feb 14 '13 at 09:08
  • 8
    You missed a major point of difference, what you can use to instantiate a generic. In c++ it is possible to use template and get a different result for any number used to instantiate it. It is used for compile time meta progaming. Like the answer in: http://stackoverflow.com/questions/189172/c-templates-turing-complete – stonemetal Aug 24 '13 at 16:32
  • 2
    You do *not* have to 'specify a type', in the form of either `extends` or `super`. Answer is incorrect, – user207421 Apr 26 '16 at 10:06
  • 2
    @EJP: You are right in saying that generics do not require you to specify a sub or super class. But in this specific example where a method is called on an object of this generic type, the generic type must have a super class (or interface) that declares this method. – klaar May 03 '16 at 14:54
  • @klaar the subclassing wouldn’t be required if the `add` interface was injected as an additional function parameter also generically parametrized on `T`. – Shelby Moore III Jan 15 '18 at 21:50
  • @ShelbyMooreIII That is very interesting! Can you concoct an example to explain this? – klaar Jan 16 '18 at 08:14
  • 1
    *"In C++ generic functions/classes can only be defined in headers, ..."* not necessarily. See this https://stackoverflow.com/a/115821 – Aykhan Hagverdili Jul 06 '20 at 13:10
  • @user207421 Actually, you have to specify the type. If you do not, it just means `extends Object`, while C++ does not have this kind of mechanism behind the scene. – Sourav Kannantha B Feb 08 '21 at 10:12
  • I know this is a old answer, just to add in `c++` you can always SFINAE or require(c++20) the constraint if you want. – apple apple Mar 24 '22 at 06:11
139

Java Generics are massively different to C++ templates.

Basically in C++ templates are basically a glorified preprocessor/macro set (Note: since some people seem unable to comprehend an analogy, I'm not saying template processing is a macro). In Java they are basically syntactic sugar to minimize boilerplate casting of Objects. Here is a pretty decent introduction to C++ templates vs Java generics.

To elaborate on this point: when you use a C++ template, you're basically creating another copy of the code, just as if you used a #define macro. This allows you to do things like have int parameters in template definitions that determine sizes of arrays and such.

Java doesn't work like that. In Java all objects extent from java.lang.Object so, pre-Generics, you'd write code like this:

public class PhoneNumbers {
    private Map phoneNumbers = new HashMap();
    
    public String getPhoneNumber(String name) {
      return (String) phoneNumbers.get(name);
    }
}

because all the Java collection types used Object as their base type so you could put anything in them. Java 5 rolls around and adds generics so you can do things like:

public class PhoneNumbers {
    private Map<String, String> phoneNumbers = new HashMap<String, String>();
    
    public String getPhoneNumber(String name) {
        return phoneNumbers.get(name);
    }
}

And that's all Java Generics are: wrappers for casting objects. That's because Java Generics aren't refined. They use type erasure. This decision was made because Java Generics came along so late in the piece that they didn't want to break backward compatibility (a Map<String, String> is usable whenever a Map is called for). Compare this to .Net/C# where type erasure isn't used, which leads to all sorts of differences (e.g. you can use primitive types and IEnumerable and IEnumerable<T> bear no relation to each other).

And a class using generics compiled with a Java 5+ compiler is usable on JDK 1.4 (assuming it doesn't use any other features or classes that require Java 5+).

That's why Java Generics are called syntactic sugar.

But this decision on how to do generics has profound effects so much so that the (superb) Java Generics FAQ has sprung up to answer the many, many questions people have about Java Generics.

C++ templates have a number of features that Java Generics don't:

  • Use of primitive type arguments.

    For example:

    template<class T, int i>
    class Matrix {
        int T[i][i];
        ...
    }
    

    Java does not allow the use of primitive type arguments in generics.

  • Use of default type arguments, which is one feature I miss in Java but there are backwards compatibility reasons for this;

  • Java allows bounding of arguments.

    For example:

    public class ObservableList<T extends List> {
        ...
    }
    

It really does need to be stressed that template invocations with different arguments really are different types. They don't even share static members. In Java this is not the case.

Aside from the differences with generics, for completeness, here is a basic comparison of C++ and Java (and another one).

And I can also suggest Thinking in Java. As a C++ programmer a lot of the concepts like objects will be second nature already but there are subtle differences so it can be worthwhile to have an introductory text even if you skim parts.

A lot of what you'll learn when learning Java is all the libraries (both standard--what comes in the JDK--and nonstandard, which includes commonly used things like Spring). Java syntax is more verbose than C++ syntax and doesn't have a lot of C++ features (e.g. operator overloading, multiple inheritance, the destructor mechanism, etc) but that doesn't strictly make it a subset of C++ either.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
cletus
  • 616,129
  • 168
  • 910
  • 942
  • They are completely different in implmentation, but equivalent ( or at least similar ) in concept, that is what tina asked. – OscarRyz Jan 31 '09 at 05:33
  • 1
    They are not equivalent in concept. The best example being the curiously recurring template pattern. The second-best being policy-oriented design. The third-best being the fact that C++ allows integral numbers to be passed in the angle brackets (myArray<5>). – Max Lybbert Jan 31 '09 at 08:40
  • 1
    No, they're not equivalent in concept. There is some overlap in the concept, but not much. Both allow you to create List, but that's about as far as it goes. C++ templates go a lot further. – jalf Jan 31 '09 at 18:56
  • 5
    Important to note that the type erasure issue means more than just backwards compatability for `Map map = new HashMap`. It means you can deploy new code on an old JVM and it will run due to the similarities in bytecode. – Yuval Adam Jan 31 '09 at 18:57
  • C++ templates have nothing to do with the preprocessors or macros. They never expand to text. – Nemanja Trifunovic Jan 31 '09 at 23:01
  • 1
    You'll note I said "basically a glorified preprocessor/macro". It was an analogy because each template declaration will create more code (as opposed to Java/C#). – cletus Jan 31 '09 at 23:03
  • Maybe it will and maybe won't create more code - modern compilers are pretty good at removing duplicated code; but the point is that template instantiation is nothing like macro text expansion - thinking in those terms can lead to nasty bugs. – Nemanja Trifunovic Jan 31 '09 at 23:58
  • I disagree. If you odn't think in those terms you may make the mistake of thinking static instance members are shared between template types, which they aren't. Template code isn't much above copy-and-paste (and I don't mean this in a bad way) hence the macro comparison. – cletus Feb 01 '09 at 00:26
  • 4
    Template code is *very* different than copy-and-paste. If you think in terms of macro expansion, sooner or later you'll be hit by subtle bugs such as this one: http://womble.decadentplace.org.uk/c++/template-faq.html#type-syntax-error – Nemanja Trifunovic Feb 02 '09 at 02:20
  • `IEnumerable` inherits from `IEnumerable`, so I wouldn't say that they bear no relation to each other. – svick Sep 24 '10 at 20:57
  • 1
    Java Generics are *not* syntactic sugar, because they extend the Java *type system*. It is true that the limitations of the JVM prevent them from offering the kinds of efficiency advantages that other languages get from parametric polymorphism, but that's an implementation concern. – dfeuer Jan 09 '15 at 07:28
  • @NemanjaTrifunovic I have used several C++ compilers where templates did indeed expand to text. – user207421 Apr 26 '16 at 10:07
  • @cletus Link you have provided for `default type arguments` just takes to IBM documentation home page. It might have been broken. Check it. – Sourav Kannantha B Feb 08 '21 at 10:22
99

C++ has templates. Java has generics, which look kinda sorta like C++ templates, but they're very, very different.

Templates work, as the name implies, by providing the compiler with a (wait for it...) template that it can use to generate type-safe code by filling in the template parameters.

Generics, as I understand them, work the other way around: the type parameters are used by the compiler to verify that the code using them is type-safe, but the resulting code is generated without types at all.

Think of C++ templates as a really good macro system, and Java generics as a tool for automatically generating typecasts.

 

Hearen
  • 7,420
  • 4
  • 53
  • 63
Shog9
  • 156,901
  • 35
  • 231
  • 235
  • 5
    This is a pretty good, concise explanation. One tweak I'd be tempted to make is that Java generics is a tool for automatically generating typecasts *that are guaranteed to be safe* (with some conditions). In some ways they're related to C++'s `const`. An object in C++ won't be modified through a `const` pointer unless the `const`-ness is casted away. Likewise, the implicit casts created by generic types in Java are guaranteed to be "safe" unless the type parameters are manually casted away somewhere in the code. – Laurence Gonsalves Mar 01 '10 at 20:13
  • 'Think of C++ templates as a really good macro system' hugely undermines the power of C++ templates – Nitesh Feb 23 '22 at 10:07
  • Hugely? I can't agree. The cases where templates are more useful than really good macros are few and far between. – barneypitt Aug 31 '22 at 13:39
17

Another feature that C++ templates have that Java generics don't is specialization. That allows you to have a different implementation for specific types. So you can, for example, have a highly optimized version for an int, while still having a generic version for the rest of the types. Or you can have different versions for pointer and non-pointer types. This comes in handy if you want to operate on the dereferenced object when handed a pointer.

KeithB
  • 16,577
  • 3
  • 41
  • 45
  • 1
    +1 template specialization is incredibly important for compile-time metaprogramming - this difference in itself makes java generics that much less potent – Faisal Vali Jun 15 '09 at 18:38
14

There is a great explanation of this topic in Java Generics and Collections By Maurice Naftalin, Philip Wadler. I highly recommend this book. To quote:

Generics in Java resemble templates in C++. ... The syntax is deliberately similar and the semantics are deliberately different. ... Semantically, Java generics are defined by erasure, where as C++ templates are defined by expansion.

Please read the full explanation here.

alt text
(source: oreilly.com)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
5

Basically, AFAIK, C++ templates create a copy of the code for each type, while Java generics use exactly the same code.

Yes, you can say that C++ template is equivalent to Java generic concept ( although more properly would be to say Java generics are equivalent to C++ in concept )

If you are familiar with C++'s template mechanism, you might think that generics are similar, but the similarity is superficial. Generics do not generate a new class for each specialization, nor do they permit “template metaprogramming.”

from: Java Generics

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
4

Java (and C#) generics seem to be a simple run-time type substitution mechanism.
C++ templates are a compile-time construct which give you a way to modify the language to suit your needs. They are actually a purely-functional language that the compiler executes during a compile.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
3

Another advantage of C++ templates is specialization.

template <typename T> T sum(T a, T b) { return a + b; }
template <typename T> T sum(T* a, T* b) { return (*a) + (*b); }
Special sum(const Special& a, const Special& b) { return a.plus(b); }

Now, if you call sum with pointers, the second method will be called, if you call sum with non-pointer objects the first method will be called, and if you call sum with Special objects, the third will be called. I don't think that this is possible with Java.

lukas
  • 2,300
  • 6
  • 28
  • 41
KeithB
  • 16,577
  • 3
  • 41
  • 45
3

The answer below is from the book Cracking The Coding Interview Solutions to Chapter 13, which I think is very good.

The implementation of Java generics is rooted in an idea of"type erasure:'This technique eliminates the parameterized types when source code is translated to the Java Virtual Machine (JVM) bytecode. For example, suppose you have the Java code below:

Vector<String> vector = new Vector<String>();
vector.add(new String("hello"));
String str = vector.get(0);

During compilation, this code is re-written into:

Vector vector = new Vector();
vector.add(new String("hello"));
String str = (String) vector.get(0);

The use of Java generics didn't really change much about our capabilities; it just made things a bit prettier. For this reason, Java generics are sometimes called"syntactic sugar:'.

This is quite different from C++. In C++, templates are essentially a glorified macro set, with the compiler creating a new copy of the template code for each type. Proof of this is in the fact that an instance of MyClass will not share a static variable withMyClass. Tow instances of MyClass, however, will share a static variable.

/*** MyClass.h ***/
 template<class T> class MyClass {
 public:
 static int val;
 MyClass(int v) { val v;}
 };
 /*** MyClass.cpp ***/
 template<typename T>
 int MyClass<T>::bar;

 template class MyClass<Foo>;
 template class MyClass<Bar>;

 /*** main.cpp ***/
 MyClass<Foo> * fool
 MyClass<Foo> * foo2
 MyClass<Bar> * barl
 MyClass<Bar> * bar2

 new MyClass<Foo>(10);
 new MyClass<Foo>(15);
 new MyClass<Bar>(20);
 new MyClass<Bar>(35);
 int fl fool->val; // will equal 15
 int f2 foo2->val; // will equal 15
 int bl barl->val; // will equal 35
 int b2 bar2->val; // will equal 35

In Java, static variables are shared across instances of MyClass, regardless of the different type parameters.

Java generics and C ++ templates have a number of other differences. These include:

  • C++ templates can use primitive types, like int. Java cannot and must instead use Integer.
  • In Java, you can restrict the template's type parameters to be of a certain type. For instance, you might use generics to implement a CardDeck and specify that the type parameter must extend from CardGame.
  • In C++, the type parameter can be instantiated, whereas Java does not support this.
  • In Java, the type parameter (i.e., the Foo in MyClass) cannot be used for static methods and variables, since these would be shared between MyClass and MyClass. In C++, these classes are different, so the type parameter can be used for static methods and variables.
  • In Java, all instances of MyClass, regardless of their type parameters, are the same type. The type parameters are erased at runtime. In C++, instances with different type parameters are different types.
Jswq
  • 758
  • 1
  • 7
  • 23
2

I will sum it up in a single sentence: templates create new types, generics restricts existing types.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    Your explanation is so brief! And makes perfectly sense for people that understand the topic well. But for people who don't understand it yet, it doesn't help very much. (Which is the case of anybody asking question on SO, got it?) – Jakub Nov 03 '16 at 15:08
1

@Keith:

That code is actually wrong and apart from the smaller glitches (template omitted, specialization syntax looks differently), partial specialization doesn't work on function templates, only on class templates. The code would however work without partial template specialization, instead using plain old overloading:

template <typename T> T sum(T a, T b) { return a + b; }
template <typename T> T sum(T* a, T* b) { return (*a) + (*b); }
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 2
    Why is this an answer and not a comment? – Laurence Gonsalves Mar 01 '10 at 20:15
  • 3
    @Laurence: for once, because it was posted long before comments were implemented on Stack Overflow. For another, because it’s not only a comment – it’s also an answer to the question: something like the above code isn’t possible in Java. – Konrad Rudolph Mar 01 '10 at 20:30
0

Templates are nothing but a macro system. Syntax sugar. They are fully expanded before actual compilation (or, at least, compilers behave as if it were the case).

Example:

Let's say we want two functions. One function takes two sequences (list, arrays, vectors, whatever goes) of numbers, and returns their inner product. Another function takes a length, generates two sequences of that length, passes them to the first function, and returns it's result. The catch is that we might make a mistake in the second function, so that these two functions aren't really of the same length. We need the compiler to warn us in this case. Not when the program is running, but when it's compiling.

In Java you can do something like this:

import java.io.*;
interface ScalarProduct<A> {
    public Integer scalarProduct(A second);
}
class Nil implements ScalarProduct<Nil>{
    Nil(){}
    public Integer scalarProduct(Nil second) {
        return 0;
    }
}
class Cons<A implements ScalarProduct<A>> implements ScalarProduct<Cons<A>>{
    public Integer value;
    public A tail;
    Cons(Integer _value, A _tail) {
        value = _value;
        tail = _tail;
    }
    public Integer scalarProduct(Cons<A> second){
        return value * second.value + tail.scalarProduct(second.tail);
    }
}
class _Test{
    public static Integer main(Integer n){
        return _main(n, 0, new Nil(), new Nil());
    }
    public static <A implements ScalarProduct<A>> 
      Integer _main(Integer n, Integer i, A first, A second){
        if (n == 0) {
            return first.scalarProduct(second);
        } else {
            return _main(n-1, i+1, 
                         new Cons<A>(2*i+1,first), new Cons<A>(i*i, second));
            //the following line won't compile, it produces an error:
            //return _main(n-1, i+1, first, new Cons<A>(i*i, second));
        }
    }
}
public class Test{
    public static void main(String [] args){
        System.out.print("Enter a number: ");
        try {
            BufferedReader is = 
              new BufferedReader(new InputStreamReader(System.in));
            String line = is.readLine();
            Integer val = Integer.parseInt(line);
            System.out.println(_Test.main(val));
        } catch (NumberFormatException ex) {
            System.err.println("Not a valid number");
        } catch (IOException e) {
            System.err.println("Unexpected IO ERROR");
        }
    }
}

In C# you can write almost the same thing. Try to rewrite it in C++, and it won't compile, complaining about infinite expansion of templates.

MigMit
  • 1,698
  • 12
  • 14
-1

I would like to quote askanydifference here:

The main difference between C++ and Java lies in their dependency on the platform. While, C++ is platform dependent language, Java is platform independent language.

The above statement is the reason why C++ is able to provide true generic types. While Java does have strict checking and hence they don't allow using generics the way C++ allows it.