I am unable to understand the below issue for type safety when using JDK 11. Can anyone explain the reason for not getting a compilation error when I am directly passing the Set.of
in the argument:
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);
}
You can run this code live at IdeOne.com.