• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write a generic swap method in Java?

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:Somehow I have a feeling that I can't use instanceOf with generic programming, is this true?

Partly, because of how generics are implemented in Java (through type erasure). You cannot write obj instanceof T where T is a type parameter. But you should avoid using instanceof anyway, because there are almost always better ways to solve problems for which you think you need it (you can often use polymorphism instead).

Jon Camilleri wrote:2. How is it possible to implement a generic swap algorithm, ...

That example code you posted is C++. In C++, you have pass-by-reference, and together with templates this makes it easy to write a generic swap method. Java does not have pass-by-reference so the same thing is simply not possible in Java.

Note also that generics in Java do not work in the same way as templates in C++. In C++, a template is really a template: when you use it with concrete arguments for the types, the compiler will generate a new class using those concrete types, which potentially leads to a lot of generated classes. In Java, a generic class is not a template. Just one class is generated with the types erased (i.e. all the generic types become Object). The compiler will check that you use it with the correct types, but at runtime the type information is gone.

Jon Camilleri wrote:3. Can a generic class throw an exception? How?

Ofcourse, in exactly the same way as a non-generic class can. There's nothing special about classes with type parameters that changes anything with regard to exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic