I am reading through a
a tutorial on generic programming, and, my first step towards that is the apparently classic example involving a Pair.
1. Is it possible to return an array, or a collection of objects?
Somehow I have a feeling that I can't use instanceOf with generic programming, is this true?
2. How is it possible to implement a generic swap algorithm, since for example,
String and (wrapped) primitive data types might use different methods in
Java to validate whether they are of the same type, and, to implement
Comparable
template<typename T>
void Swap(T & a, T & b) //"&" passes parameters by reference
{
T temp = b;
b = a;
a = temp;
}
string hello = "world!", world = "Hello, ";
Swap( world, hello );
cout << hello << world << endl; //Output is "Hello, world!"
3. Can a generic class throw an exception? How?
Sourced from
here.
Other sources of reference
1.
Angelika Langer, Generic and Parameterized Types
2.
Core Java Volume I 8th Edition Chapter 12. ISBN-13 978-0-13-235476-9.
Upon first reading, the programming notes were not very clear to me, does anyone have a copy of this book, and, might provide feedback in this regard?
3.
Using and Programming Generics in J2SE 5.0
4.
C. Mueller and S. Jensen: The Java Generic Programming System.
5.
The instanceOf keyword