and how to avoid it w/o
Don't do unnecessary casts up and down the class hierarchy.
Wrong.
To understand the warning, you need to understand how generics work.
While at compile time generic types are full, meaning Vector<String> is
not Vector<Integer>, at runtime this distinction goes away, meaning
Vector<String> is Vector<Integer>. The process is called erasure and it
is done to remain backward compatible with Java 1.4 jvm (Code with
generics but no other 1.5 features, compiled with jdk 1.5, will run on
jvm 1.4).
How does this affect you?
At compile time you enjoy the generic type safety telling you that
"new Vector<Integer>().add("hi!");" will not work.
But (and there's always a "but"), at runtime there is no way to
distinguish between Vector<String> and Vector<Integer>. That means that
the compiler can't tell you you're doing a mistake. So:
"Vector<String> vs = ((Vector<String>) ((Object) new
Vector<Integer>));" would work. However, the next sequence would result
in a casting exceptions:
"vs.add(123); String s = vs.elementAt(0);", although generic wise it's
"supposed" to be safe (since vs is Vector<String>). To tell you this,
the compiler emits the warning you saw.