我正在学习Java @SafeVarargs。我创建了一个方法,接受不同长度的int数组作为参数并调用该方法。但是,NetBeans没有显示有关不安全使用可变长度参数的任何警告。为什么不显示警告?有人可以告诉我吗?
static void safeVarargsMethod(int... a){
System.out.println(Arrays.toString(a));
}
public static void main(String[] args) {
DeclaringAnAnnotationType.safeVarargsMethod();
}
答案 0 :(得分:1)
此代码(taken from the JavaDoc)演示了NetBeans 8.0.2(以及Eclipse Mars.2)中的问题:
static void m(List<String>... stringLists) {
Object[] array = stringLists;
List<Integer> tmpList = Arrays.asList(42);
array[0] = tmpList; // Semantically invalid, but compiles without warnings
String s = stringLists[0].get(0); // Oh no, ClassCastException at runtime!
}
我认为问题在于您的示例首先没有证明问题。