Sunday, December 4, 2011

Motivation for java Generics + C++ template

 
 
http://en.wikipedia.org/wiki/Generics_in_Java 

Motivation for generics

The following block of Java code illustrates a problem that exists when not using generics. First, it declares an ArrayList of type Object. Then, it adds a String to the ArrayList. Finally, it attempts to retrieve the added String and cast it to an Integer.

List v = new ArrayList();
  v.add("test");
  Integer i = (Integer)v.get(0);        // Run time error

Although the code compiles without error, it throws a runtime exception (java.lang.ClassCastException) when executing the third line of code. This type of problem can be avoided by using generics and is the primary motivation for using generics.
Using generics, the above code fragment can be rewritten as follows:
 
List<String> v = new ArrayList<String>();
  v.add("test");
  Integer i = v.get(0); // (type error)  Compile time error
 
 
 http://stackoverflow.com/questions/36347/what-are-the-differences-between-generic-types-in-c-and-java
 
1.  
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.

<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 ); }
 
... 
 
2.  
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. 
 
... 
 
3. 
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];
 
...
}
  • Use of default type arguments, which is one feature I miss in Java but there are backwards compatibility reasons for this;
  • C++ allows the use of primitive type arguments, Java doesn't; and
  • Java allows bounding of arguments.
For example:
public class ObservableList<T extends List> {
 
...
}


4. 


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.
 http://books.google.com.au/books?id=3keGb40PWJsC&dq=java+generics+and+collections&printsec=frontcover&source=bn&hl=en&sa=X&oi=book_result&ct=result&redir_esc=y#v=onepage&q&f=false


No comments:

Post a Comment