today i start learning generic.
i m from c and C++ background so i have some knowledge about template and in java the generics is similar to template in c++.
right?
look at my code carefully .....
-
public class MyGenericClass<e>
-
{
-
private e data;
-
public MyGenericClass(e d){data = d;}
-
public e getData()
-
{
-
return e;
-
}
-
}
-
-
//With this code no problem
-
//but the problem is when i access this class.
-
//this is the code part
-
-
MyGenericClass<int> c = new MyGenericClass<int>(100);
-
-
//Here the compiler tells that the type is not found.
-
//I understand primitive type is not supported, only class or interface type is //suppoerted.
-
could anyone tell me why sun did this?
kind regards,
dmjpro.
Generic types are types, not primitives; you can't use 'int' for the puropse, you
have to use the 'Integer' wrapper class for that. Note that autoboxing takes away
most of the notational burden. Generic classes are very much unlike class templates,
i.e. a class template is instantiated to a templatized class as soon as you 'use'
(parts of) that class; it's named the 'POI' (Point of Instantiatiion); due to C++'s
separate compilation the same (parts of a) class can be instantiated over and
over again if you're not careful ("code bloat").
Generic classes don't instantiate anything at all: it's just the compiler that uses
a primitive class as if it were coupled to the type parameters and it checks for
it. After compilation is over and done, the type parameter(s) is/are said to be
'erased' from the class and all that is left is the same primitive class, e.g. List<E>
and List<F> are/is the same List and no code is generated or instantiated.
kind regards,
Jos