使用 JDK 11 时,我无法理解以下类型安全问题。谁能解释当我直接传递Set.of
参数时没有收到编译错误的原因:
public static void main(String[] args) {
var intSet1 = Set.of(123, 1234, 101);
var strValue = "123";
isValid(strValue, intSet1);// Compilation error (Expected behaviour)
**isValid(strValue, Set.of(123, 1234, 101));// No Compilation error**
}
static <T> boolean isValid(T value, Set<T> range) {
return range.contains(value);
}
Run Code Online (Sandbox Code Playgroud)
您可以在 IdeOne.com 上实时运行此代码。