Item 32. Varargs
class Main {
public static void main(String[] args) {
dangerous(List.of("There be dragons!")); // Warning, Unchecked generics array creation for varargs parameter
}
// Warning, Possible heap pollution from parameterized vararg type
private static void dangerous(List<String>... stringLists) {
List<Integer> intList = List.of(42);
Object[] objects = stringLists;
objects[0] = intList; // ν μ€μΌ λ°μ
String s = stringLists[0].get(0); // νλ³ν λμ€ ClassCastException λ°μ
}
}κ°λ³μΈμ μλ¬κ° λ°μνμ§ μλ μ΄μ
@SafeVarargs
List.of() μ¬μ©
List.of() μ¬μ©Last updated