Java Generics: Taking the fun away from writing code in Java

Coding in Java was simple until Java 5 (or Java 1.5 — 1.5 is the developer version and Java 5 is the marketing version as Sun calls it!). Learning generics in Java 1.5 is like learning Microsoft COM programming, it would take at least 5 passes to absorb it right.

Look at this simple call, before generics

1 List myIntList = new LinkedList(); 
2 myIntList.add(new Integer(0)); 
3 Integer x = (Integer) myIntList.iterator().next(); 

How about now:

1 List<Integer> myIntList = new LinkedList<Integer>(); 
2 myIntList.add(new Integer(0)); 
3 Integer x = myIntList.iterator().next(); 

Here is the source snippet of java.util.Collection class from the 1.5 version. Makes me wipe the sweat.

1 public interface Collection<E> extends Iterable<E> {
2 <T> T[] toArray(T[] a);
3 boolean containsAll(Collection<?> c);
4 boolean addAll(Collection<? extends E> c);
5 Iterator<E> iterator();
6 }

Yeah, yeah. The fans of C++ would love it, it looks like the C++ templates, compile-time type checking, etc. — Well, programmers shall figure out other ways of making mistakes like working on a null object :D)
No way out, I need to learn it ‘coz 1.6 is already out and I was still hanging on to 1.4 till yesterday!

Comments are closed.