File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to write a generic swap method in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to write a generic swap method in Java?" Watch "How to write a generic swap method in Java?" New topic
Author

How to write a generic swap method in Java?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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


Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write a generic swap method in Java?