【发布时间】:2014-10-14 11:01:15
【问题描述】:
我预计后续单元测试会因 ClassCastException 而失败,但它会通过。
该类有一个泛型方法,其第二个参数和返回值的类型为 V。
第二次调用该方法时,第二个参数V的类型为Integer,返回类型应为Integer。但在运行时它实际上返回字符串值。
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class GenericMethodTest {
private class NonGenericClass {
private final Map<Object, Object> myMap = new HashMap<>();
<K, V> V addGeneric(K key, V value) {
V existingV = (V) myMap.get(key);
// why no ClassCastException on this above line, when type of V is Integer, but myMap.get(key) returns value of
// type String?
if (existingV == null) {
myMap.put(key, value);
return value;
}
return existingV;
}
}
@Test
public void test() {
NonGenericClass nonGenericClass = new NonGenericClass();
nonGenericClass.addGeneric("One", "One");
// String valueString = (String) nonGenericClass.addGeneric("One", Integer.valueOf(1));
// Compiler error as expected, if above line uncommented - Cannot cast from Integer to String.
// But no error at run-time, and below call returns value of type String.
nonGenericClass.addGeneric("One", Integer.valueOf(1));
}
}
【问题讨论】: