@NotNull and varargs
已回答
Hello, why in this code :
String[] l = {"A", null, "B"};
f(l);
private void f(@NotNull String... args)
{
for (String i : args)
{
if (i != null) System.out.println(i);
else System.out.println("null");
}
}
i != null is highlighted by the 'Constants conditions & exceptions' inspection ('i != null' always true) ?
Because if i run this code it prints "A null B", thus the second line is reached, thus 'i != null' is not always true.
请先登录再写评论。
Because apparently you're using a TYPE_USE-compatible @NotNull annotation, and in JDK 8 this syntax means that the array's elements are required to be notnull. Unfortunately the call isn't currently checked, because "l" is declared separately from "f(l)". If it were a vararg call, IDEA 2017.1 preview would highlight the null.
See also https://youtrack.jetbrains.com/issue/IDEA-167067
Ok, thank you.