While building large-scale projects, we need the code to be compatible with any kind of data which is provided to it. That is the place where your written code stands above that of others. Here what we meant is to make the code we write be generic to any kind of data provided to the program regardless of its data type. This is where Generics in Java and the similar in C++ named Template come in handy. While both have similar functionalities, but they differ in a few places.
The main difference between Templates in C++ and Generics in Java is show below:
- C++ templates create new code for each type we use.
- Java generics only check types when we write the code but use the same code at run-time.
C++ Templates vs Java Generics
The table below demonstrates the difference C++ Templates vs Java Generics
Features | C++ Templates | Generics in Java |
|---|---|---|
Code generation | It generates new code for each type. | In this no new code generated. |
Runtime overhead | There is runtime overhead | There is no overhead |
Type safety | It is less strict | It is more strict |
Support for primitives | It provides support for primitives | It requires wrapper classes |
Metaprogramming | It provides support for metaprogramming | It does not provide support for metaprogramming |
Template in C++
Writing Generic programs in C++ is called Templates. One of the major features of the template in C++ is the usage of metaprogramming. It let's the template signature of the provided code be different, were C++ provides the ability to implement them.
- Template arguments can be types classes or values.
- Templates must be defined in headers so the compiler can generate code when used.
- Template specialization allows implementing specific behaviors for particular types.
Example:
// CPP program to illustrate Templates
#include <iostream>
#include <string.h>
using namespace std;
template <class T>
class TempClass {
T value;
public:
TempClass(T item)
{
value = item;
}
T getValue()
{
return value;
}
};
int main()
{
class TempClass<string>* String =
new TempClass<string>("Generics vs Templates");
cout << "Output Values: " << String->getValue()
<< "\n";
class TempClass<int>* integer = new TempClass<int>(9);
cout << "Output Values: " << integer->getValue();
}
Output
Output Values: Generics vs Templates Output Values: 9
Generics in Java
One of the major features of Java Generics is that It handles type checking during instantiation and generates byte-code equivalent to non-generic code. The compiler of Java checks type before instantiation, that in turn makes the implementation of Generic type-safe. Meanwhile, in C++, templates know nothing about types.
- If Generics is applied in a class, then it gets Applied to classes and methods within classes.
- Another major factor that leads to the use of generics in Java is because it allows you to eliminate downcasts.
- Instantiating a generic class has no runtime overhead over using an equivalent class that uses as specific object rather than a generic type of T.
Example:
// Java program to illustrate
// Generics
public class GenericClass<T> {
private T value;
public GenericClass(T value)
{
this.value = value;
}
public void showType()
{
System.out.println("Type:" +
value.getClass().getSimpleName());
System.out.println("Value: " + value);
}
public static void main(String[] args)
{
GenericClass<String> Str =
new GenericClass<String>("Generics vs Templates");
GenericClass<Integer> integer =
new GenericClass<Integer>(9);
Str.showType();
integer.showType();
}
}
Output
Type:String Value: Generics vs Templates Type:Integer Value: 9