Donnerstag, 16. April 2009
Java type system anomally
In the last days I was hacking a small system integration application in Scala. It was really a pleasure to work with a reasonable type system. During that I remembered several of the “well known” Java Type anomalies especially following code fragment:

Integer ints[]=new Integer[1];
Object objs[]=ints;
objs[0]="Hallo Arno";

A really nice one, which I really like. It compiles without any warnings, but it is completely broken. For inexperienced ( Java) developers maybe a little hard to spot why. It would be easy and nice to create a nice Java Puzzle out of this theme. The java-generics documentation states,

“Because the program compiles without warnings, we can state with certainty that it will not throw a ClassCastException at run time.”

This is right, because the code throws a java.lang.ArrayStoreException. So the statement is formally correct, but in practice, there is no difference. In any case the method terminates with a runtime exception. On the other hand I understand the initial design forces for the java team. They wanted a simple typed language and they wanted to be able to write some array handling library code.

I haven´t made up my mind jet, which solution to this dilemma I would prefer. These are:

  1. The highly sophisticated inheritance model of the latest languages like scala
  2. the dynamic duc typing of languages with a pure dynamic type system like J/Python, J/Ruby,
But for one think I am sure, I prefer both solutions over the pure old fashioned Java type system. What do you think?

... link (1 Kommentar)   ... comment