C ++ Templates and Generic Types in Java: The Differences

Do you need to find out how C ++ Templates are different from Generic Types in Java? Be quick to get acquainted with the example below to find out the core differences. From this sample, you will know how the Templates and the Generic Types are implemented in each of the languages. Hopefully, this information will help you to cope with your task. Our service can provide even more samples on the topic if you need.

AssignmentShark is a service which has gathered experts in important fields, such as math, chemistry, biology, physics, and IT. Students who have difficulties with any of their technical tasks are welcome to apply for help. Our experts possess vast knowledge in these spheres and can work under the pressure of time due to their vast experience in completing academic assignments. So, if you need to get more information about Generic Types in Java, you can make the order right away and we will definitely help you!

The Difference Between the Templates in C ++ or Generic Types in Java

Many programmers think that C ++ Templates and Generic Types in Java are the same things because the syntax is similar: in both cases, we can write something like “List <T>”. To find the differences, let’s see how the Templates and the Generic Types are implemented in each of the languages.

Java Generic Types are associated with the idea of “type erasure”. This technique eliminates the type parameters when the source code is converted into the JVM bytecode.

For example, we have a Java code:

 
Vector vector = new Vector();
vector.add(new String("hello"));
String str = vector.get(0);
During the compilation, it will be converted:
Vector vector = new Vector();
vector.add(new String("hello"));
String str = (String) vector.get(0);

The use of Java generalizations did not affect our abilities, but it made the code more beautiful. That’s why the Generic Types in Java are often called “syntactic sugar”.

The Generic Types are very different from C ++ Templates. The Template in C ++ is a set of macros that create a new copy of the template code for each type. This is particularly evident in the following example: an instance of MyClass <Foo> will not be able to use a static variable together with MyClass <Bar>. Two instances of MyClass <Foo> will use a static variable together.

To illustrate this example, let’s take a look at the following code:

/*** MyClass.h ***/
template<typename T>
class MyClass {
public:
  static int value;
  MyClass(int val){ value = val; }
};
 
/*** MyClass.cpp ***/
template<typename T>
int MyClass<T>::value;
 
template class MyClass<Foo>;
template class MyClass<Bar>;
 
/*** main.cpp ***/
MyClass * foo1 = new MyClass<Foo>(1);
MyClass * foo2 = new MyClass<Foo>(2);
MyClass * bar1 = new MyClass<Bar>(3);
MyClass * bar2 = new MyClass<Bar>(4);
 
int f1 = foo1->value; // 2
int f2 = foo2->value; // 2
int b1 = bar1->value; // 4
int b2 = bar2->value; // 4

In Java, various instances of MyClass can share the static variables, regardless of the type of parameters.
Because of differences in the architecture, Java Generic Types and C ++ Templates have a number of differences:

  • C ++ Templates can use primitive types, such as int, and Java Generic Types can not; they have to use Integer.
  • Java allows specifying the limits on the type passed as a parameter. For example, you can use Generic Types to implement CardDeck and indicate that the type of parameter should be inherited from CardGame.
  • In C ++ you can create an instance of a type passed as a parameter, but in Java you can’t do this.
  • Java does not allow types passed as parameters (for example Foo in MyClass <Foo>) for static methods and variables because they can be shared in MyClass <Foo> and MyClass <Bar>. In C ++ these classes are different, so the type of the parameter can be used for static methods and variables.

So while Java Generic Types and C ++ Templates look similar, they are different things.

Thanks for your attention!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.