We require the code to be adaptable to various data types for the development of extensive projects. This is where the uniqueness of your code implementation shines through compared to others. Essentially, the code should be designed to handle any kind of data input without being specific to a particular data type. This is where Generics in Java and Templates in C++ prove to be advantageous. Despite serving similar purposes, they exhibit differences in their functionality.
C++ Template
Templates are used in C++ to create generic programmes.
- The use of metaprogramming is a key feature of the template in C++. It allows the provided code's template signature to be different, where C++ provides the ability to implement them.
- Both classes and functions can be used as template arguments.
- Template sources must be included in C++ headers.
- Template specialisation is possible, which means that a specific type and method of template can be implemented.
Program of C++:
// 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
Java Generics
- One of the most important characteristics of Java Generics is that it handles type checking during instantiation and generates byte-code that is equivalent to non-generic code.
- Because the Java compiler checks type before instantiation, the implementation of Generic is type-safe. Meanwhile, in C++, templates have no concept of types.
- When Generics is used in a class, it is applied to all classes and methods within those classes.
- Another important reason for using generics in Java is that it allows you to eliminate downcasts.
- There is no runtime overhead to instantiating a generic class over using an equivalent class that uses a specific object rather than a generic type of T.
Program of JAVA
// 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
Generics vs. C++ Templates in Java
Though both methods for creating a generic type are similar, they differ in some places while sharing the same implementation property.
- Type erasure: Type erasure ensures a more stringent type check during compilation. Java generics merely provide compile-time safety while eliminating the need for casts. This is done directly in the Java compiler front-end and ensures type erasure.
- When you use a template in C++, the compiler will re-emit the template code after replacing the generic parameter with the type you used. This is more powerful in many ways, but it can result in bloated executables.
- Wrapper class: In Java, even if we need to specifically specify the datatype within which the function call is made using any object, we don't need to cast it like in C++ with actual data types; instead, we use wrapper classes to accomplish this.
- Type checking: During instantiation, Java Generics handles type checking and generates byte-code that is equivalent to non-generic code. C++ has "latent typing" and template metaprogramming, and each instantiation generates a new class.