+ 1
What is exactly raw type?and why not to use it?
People keep saying don't use raw type,but why?
2 Antworten
+ 4
This link actually gives a really good example.
http://www.javapractices.com/topic/TopicAction.do?Id=224
Raw types are generic types used without type parameters, so
List obj;
is a raw type instead of
List<String> obj;
List<Integer> obj;
etc
which are parameterized types.
Why is this bad? We can look at the code snippet:
void badPrint(List obj){
Iterator iter = obj.iterator();
while(iter.hasNext())
System.out.println((String) iter.next());
}
In this case, we never really know if the code works. It may throw a ClassCastException, it may not. This could be avoided if we specified a class which is definitely cast-able to String in the type parameter, instead of letting your function accept a generic List.
More comprehensive answers can be found on SO and many other articles. I suggest doing further reading.
https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it
+ 1
raw types have been used before Java 5 and are deprecated now because generic types are safer in type