Java Generics - Be careful with SubTypes

In Java versions before the Tiger (Java 5.0) a Collection as we know it, will only contain Objects . The compiler knows that any Object can be put in the collection. The inability of the compiler to identify the intent of the programmer to use a collection of a particular type probably caused a lot of time spent on debugging ClassCastExceptions.

Hello and Welcome Generics
Generics in Java 5.0 (and versions after) will allow you to define a type for a Collection.
The use of generics is not limited for typing Collections but I will only touch on this subject because it brings me great joy to know that i can express during compile time that i want a certain type of Collection. It was really a pain to experience it during runtime, specially when you need to deploy an application.

Before Java 5.0
Sample code for a Java Collection before Java 5.0
Here is a sample code on how a Collection is used before Java 5.0
1 List listFoo = new ArrayList() ;
2 listFoo.add(new Foo());
3 Foo aFoo = (Foo) listFoo.iterator().next();

You need to do the cast (line 3) when retrieving Objects from a Collection. The thing about this is that we know the type of Object that we added to the Collection but we still need to do a cast in order to use it as the type of Object that we know it is, because the compiler can just guarantee that an Object will be returned when we use an Iterator to retrieve the Objects in the Collection.

Here is what is amazing in Java 5.0
Here is a sample code:
1 List<Foo> listFoo = new ArrayList<Foo>();
2 listFoo.add(new Foo());
3 Foo aFoo = listFoo.iterator().next();

You can declare and specify a type of Collection that is checked by the compiler. In the sample code, listFoo variable is not just a List it is a List of Foo. Once you declare this, we do not need to the casting anymore. This greatly improves readability of the code. When we see a line like this we know that it is a Collection of a certain type of Objects which is clearly stated in the line of code.

Be careful of SubTypes when Using Generics
In Java we have subtyping, every object is a sub-type of java.lang.Object that is why we can do the following:
1 String s = new String("This is a String");
2 Object o = s;
We can assign sub-types to variables that are declared as super-types of an object.
When using a typed Collection we will get a compile time error when we to the following:
1 List <String>listOfString = new ArrayList <String>();
2 List<Object>listOfObject = listOfString;
Line 2 of the above code will result in compile time error. The reason for this is simple. We do not want a Collection of String to have an Object that is not a String.

So what if you don't know what type of Collection that you need during compile time ?
Generics in Java 5.0 has a wilcard type. We can declare a Collection of an unknown type.
With the unknown type you can now do this:

1 Collection<?> col = new ArrayList <String>();
2 col = new ArrayList<Object>();

Ok. that is it for now. Please refer to Java 5.0 Generics for more information.

Comments

Popular posts from this blog

OAuth 1.0a Request Signing and Verification - HMAC-SHA1 - HMAC-SHA256

Spark DataFrame - Array[ByteBuffer] - IllegalAurmentException

Gensim Doc2Vec on Spark - a quest to get the right Vector