, , U extends Foo<String>, () , Bar<U> Bar<Foo<String>>. , Bar<U> Bar<Foo<String>>, , .. U Foo<String>.
, , () List<String> List<Object>, , . List<String> List<? extends Object>, List<Object>. (, Comparable<T>: Comparable<String> " String, Comparable<Object> " Object ". , Comparable<String> Comparable<Object>.)
[& hellip;] [& hellip;], ?
, . Eclipse , , , . , :
final Object o = Integer.valueOf(7);
final String s = (String) o;
absolutely safe because the throw throws an exception. But this code:
final List<?> wildcardList = new ArrayList<Integer>(Integer.valueOf(7));
final List<String> stringList = (List<String>) wildcardList;
is unsafe because the runtime does not have the ability to check the cast (due to erasure), so it will not throw an exception, although this is not true: stringListnow it is List<String>whose first element is of type Integer. (What happens, at some point later, you may get spontaneous ClassCastExceptionwhen you try to do something with this element.)
ruakh source
share