Y
York Werres
Hi,
Gilad Bracha writes in chapter 7.3 of "Generics in the Java Programming
Language" this:
In particular, the language is designed to guarantee
that if your entire application has been compiled
without unchecked warnings using javac -source 1.5,
it is type safe.
But, believe it or not, my *very first* experiment at all with Java 1.5 was
this:
List<? super Number> list = new ArrayList<Serializable>();
list.add(new Double(0.0));
Integer theInt = (Integer) list.get(0);
I compiled it with "-source 1.5" (JDK 1.5.0 beta 1) and there were no
warnings at all. I then ran it and got a ClassCastException (as expected).
Why exactly is this called "type safe"?
Or to put the question another way: This is a bug, right?
My explanation attempt: The "? super Number" is a contra-variant bounded
wildcard which should be write-only. The compiler should not allow me to do
the "list.get(0)". The cast to Integer is ok, but calling the get is not
(because of it's return type).
Is this a known bug or do I have to report it somewhere?
BTW: This really is my very first experiment with Java generics and it
doesn't make me happy at all. What good are generics if they're not type
safe?
2004-05-17, York.
Gilad Bracha writes in chapter 7.3 of "Generics in the Java Programming
Language" this:
In particular, the language is designed to guarantee
that if your entire application has been compiled
without unchecked warnings using javac -source 1.5,
it is type safe.
But, believe it or not, my *very first* experiment at all with Java 1.5 was
this:
List<? super Number> list = new ArrayList<Serializable>();
list.add(new Double(0.0));
Integer theInt = (Integer) list.get(0);
I compiled it with "-source 1.5" (JDK 1.5.0 beta 1) and there were no
warnings at all. I then ran it and got a ClassCastException (as expected).
Why exactly is this called "type safe"?
Or to put the question another way: This is a bug, right?
My explanation attempt: The "? super Number" is a contra-variant bounded
wildcard which should be write-only. The compiler should not allow me to do
the "list.get(0)". The cast to Integer is ok, but calling the get is not
(because of it's return type).
Is this a known bug or do I have to report it somewhere?
BTW: This really is my very first experiment with Java generics and it
doesn't make me happy at all. What good are generics if they're not type
safe?
2004-05-17, York.