R
Roedy Green
Is this true?
If I take a simple container class e.g .
public class Stack
and put <t> in like this
public class Stack<T>
and replace every word "Object" with "T" in the class body I will
have a new checked version of the container class.
Oddly if I examine the byte codes generated they are exactly the same
as the old ones. Nothing has changed.
Everything still works inside with objects.
There are no casts added anywhere.
There are no type checks added anywhere.
However if you look at he byte code of the class that USES you checked
class, you will see it had added cast checks on parameters of method
calls to the container, and cast conversions of object results back to
the specific type.
The compiler knows the precise type of the call an compile time, so it
can hard code these in with the correct types.
This is a rinky dink way do to generics.
Some of the restrictions then are:
1. the container cannot construct new Objects of type T, only objects.
2. It cannot construct arrays of new Objects of type T, only Object[].
3. The container has no access to T methods other than those of
Object. It is a pure container. It cannot do anything clever with the
objects it contains.
However when you use the <X extends Dog> syntax then the code
generated for the container is done internally with Dog references.
That gives the container access to the Dog methods, but not the Dog
constructor or the constructors of any of the subclasses of Dog,
except by heroic measures using reflection or getClass.newInstance.
without constructor parameters.
the big drawback then of Java's way of doing Generics is you can't
create new objects of type T or arrays of objects of type T.
Oddly you can create objects explicitly of type Dog or Dalmatian, but
not the generic T.
You then puzzle how does ArrayList manage to allocate an array to hold
its objects internally? It cheats. it allocates an array of Objects
and does and illegal cast to (T[]) which conveniently just triggers a
warning message. It is all objects inside anyway.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
If I take a simple container class e.g .
public class Stack
and put <t> in like this
public class Stack<T>
and replace every word "Object" with "T" in the class body I will
have a new checked version of the container class.
Oddly if I examine the byte codes generated they are exactly the same
as the old ones. Nothing has changed.
Everything still works inside with objects.
There are no casts added anywhere.
There are no type checks added anywhere.
However if you look at he byte code of the class that USES you checked
class, you will see it had added cast checks on parameters of method
calls to the container, and cast conversions of object results back to
the specific type.
The compiler knows the precise type of the call an compile time, so it
can hard code these in with the correct types.
This is a rinky dink way do to generics.
Some of the restrictions then are:
1. the container cannot construct new Objects of type T, only objects.
2. It cannot construct arrays of new Objects of type T, only Object[].
3. The container has no access to T methods other than those of
Object. It is a pure container. It cannot do anything clever with the
objects it contains.
However when you use the <X extends Dog> syntax then the code
generated for the container is done internally with Dog references.
That gives the container access to the Dog methods, but not the Dog
constructor or the constructors of any of the subclasses of Dog,
except by heroic measures using reflection or getClass.newInstance.
without constructor parameters.
the big drawback then of Java's way of doing Generics is you can't
create new objects of type T or arrays of objects of type T.
Oddly you can create objects explicitly of type Dog or Dalmatian, but
not the generic T.
You then puzzle how does ArrayList manage to allocate an array to hold
its objects internally? It cheats. it allocates an array of Objects
and does and illegal cast to (T[]) which conveniently just triggers a
warning message. It is all objects inside anyway.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes