Templates in C++ vs Generics in Java
As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is to introduce you all to Generics in java and templates in C++.
While we are building some large-scale projects in organizations, we have a requirement that they have to be compatible with any input or any real-time test case that a user of the end application, whether it is in the form of a website or an application which is Android or IOS be it has to be universal to take any input to perform necessary job or function which it is assigned to do and give us a result applicational success.
Generics in Java or Templates in C++ essentially mean the same they support universal functionality with differing in very few cases; these are explained straightforwardly in the below code covering two different principles for each.
The C++ programming language requires us to template the sources to be added in their header places. While the Generics in Java programming language, when applied in a particular type of class then it will essentially get to be applied to the classes and the methods within that class.
C++ code (Templates-1)
//This is the C++ program we have written to
//to illustrate and demonstrate the Templates use case along with outputs
#include <iostream>
#include <string.h>
using namespace std;
template <class Bh>
class temp_class {
Bh value;
public:
temp_class(Bh item)
{
value = item;
}
Bh getValue()
{
return value;
}
};
int main()
{
class temp_class<string>* String =
new temp_class<string>("Game between generics vs templates here ");
cout << "Output Values: " << String->getValue()
<< "\n";
class temp_class<int>* integer = new temp_class<int>(19);
cout << "Output Values: " << integer->getValue();
}
Output
Output Values: Game between generics vs templates here
Output Values: 19
Java code (Generics-1)
//This is the java program we have written to
//to illustrate and demonstrate the Templates use case along with outputs
public class GenericMethodTest {
//here we are passing generic method printArray
public static < E > void printArray( E[] inputArray ) {
//here we are passing display array elements
for(E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
public static void main(String args[]) {
// here we are creating arrays of Integer, Double and Character
Integer[] int_Array = { 211, 32, 73, 84, 765 };
Double[] double_Array = { 11.21, 52.2, 73.3, 744.24 };
Character[] char_Array = { 'D', 'U', 'N', 'I', 'A' };
System.out.println("Here the Array integerArray contains:");
printArray(int_Array); //here we are passing an integer array
System.out.println("\nHere the Array doubleArray contains:");
printArray(double_Array); //here we are passing a Double array
System.out.println("\nHere the Array characterArray contains:");
printArray(char_Array); //here we are passing a Character array
}
}
Output
Here the Array integerArray contains:
2113273 84 765
Here the Array doubleArray contains:
11.2152.2 73.3 744.24
Here the Array characterArray contains:
D U N I A
C++ code (Templates-2)
//This is the C++ program we have written to
//to illustrate and demonstrate the Templates use case along with outputs
#include <iostream>
#include <string>
using namespace std;
template <typename Bh>
inline Bh const& Max (Bh const& a, Bh const& b) {
return a < b ? b:a;
}
int main () {
int hi = 239;
int bye = 120;
cout << "Max(hi, bye): " << Max(hi, bye) << endl;
double f1 = 113.50;
double f2 = 120.722;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;
string s1pro = "Hello";
string s2pro = "Duniya";
cout << "Max(s1pro, s2pro): " << Max(s1pro, s2pro) << endl;
return 0;
}
Output
Max(hi, bye): 239
Max(f1, f2): 120.722
Max(s1pro, s2pro): Hello
Java code (Generics-2)
class Main_here {
public static void main(String[] args) {
// initialize generic class
// with Integer data
generics_class<Integer> int_Obj = new generics_class<>(15);
System.out.println("generic class returns: " + int_Obj.getData());
System.out.println();
// initialize generic class
// with String data
generics_class<String> stringObj = new generics_class<>("Java Programming language is awesome");
System.out.println("generic class returns: " + stringObj.getData());
}
}
// create a generics class
class generics_class<bTs> {
// variable of T type
private bTs data;
public generics_class(bTs data) {
this.data = data;
}
// method that return T type variable
public bTs getData() {
return this.data;
}
}
Output
generic class returns: 15
generic class returns: Java Programming language is awesome