Generics shows that Java is ... - No Fluff Just Stuff

Generics shows that Java is ...

Posted by: Venkat Subramaniam on May 8, 2006

A few years ago I strongly believed in type safety. My opinions have changed. With the help of unit
testing, by writing and testing the code very frequently (in minutes rather than hours), I have come to
depend less on the crutches provided by the compilers. Furthermore, I have started to see how good - 
dynamic type languages provide me the flexibility, ease, and expressive power that I find strongly typed
languages don?t.

I used to do most of my development in C++. When I picked up Java (and then C#) I found that I was
more productive with these post pattern languages. Now, I am seeing languages that are lot more
productive than Java and C#.

Anyways, to the topic I wanted to discuss here. I am in Denver speaking at the NFJS event and on
Saturday during the Birds of Feather session and  during the panel discussions on Sunday, questions
related to type-safety, Generics in Java, and usage of Checked  vs. Unchecked exceptions came up.

I have spoken about Generics in Java and the illusions around it in my Good, Bad, and Ugly of Java
Generics
talk (also see my three part article Generics in Java here). But, don't take my words for it.
Ken Arnold calls Generics "a fundamental language design problem."

Generics in Java doesn?t solve any significant problem. When you compared the cost (time and effort
to learn it and dealing with the problems from the false illusions it creates), it does not really provide
benefit.

Someone argued that the code is type safe when you use Generics in Java. Here is a simple example
that shows that's not true.

ArrayList<Integer> myList = new ArrayList<Integer>();

myList.add(1);
myList.add(2);

ArrayList trouble = myList;
trouble.add("trouble");

int total = 0;
for(int val : myList)
{
total += val;
}

System.out.println("total = " + total);

You got runtime exceptions if you dealt with types incorrectly. You always did and you continue to do.
When you compile and run the above program, you will get something like:

Exception in thread "main" java.lang.ClassCastException: java.lang.String
at com.agiledeveloper.TestRunner.main(TestRunner.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)

Generics in Java does not guarantee compile time type safety. You may say that it has some minor
advantages (like your IDE can provide a better code prompt or code completion). I ask you to think
if you really needed something as heavy as Generics for that? Could there be other techniques that
can help with type inference for the IDE to help us?

Oh, but the way, talking about generics, now look at the following statement from the above example:

ArrayList<Integer> myList = new ArrayList<Integer>();

You had to say Integer two times, and it still didn?t get it (as seen from the failure). When you repeat
something over and over, and if someone still don?t get it, you may get mad and yell that they are
retarded? Well, with the usage of Generics, it give me the impression that Java is? retarded.

Venkat Subramaniam

About Venkat Subramaniam

Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com. You can reach him by email at venkats@agiledeveloper.com or on twitter at @venkat_s.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »