让我们在可变参数和空值之间拆分问题。
可变参数
可变参数的重点是发送数据数组,而不是在调用者的代码中将它们作为数组,而是作为单独的变量或常量。
调用可变参数可能不是很直观,以下是各种情况下发生的情况:
method("", "1", "2"); // vargs is {"1", "2"}
method(""); // vargs is {}, the empty array (it is not null)
method("", null); // vargs is {null}, size 1 array containing the element 'null'
method("", (Object[])null); // vargs is null, a null instance
请注意,第三种情况被认为是错误的形式。例如,如果 null 是常量(未存储在变量中),您会收到警告。
请注意,在第四种情况下,您确实在寻找问题!;-)
空类
Now, we are talking of an array that contains a null value, not about a null array (that was sorted out in the previous part).
General case
Null can be of any class (all at a time). But instanceof
will always return false.
Put into a map
If one value is null, you need to think about what you want to do. Obviously, getClass() cannot be called on a null value. So you can choose between:
- skip the null value, not add it in the map
- choose a class you want to associate to null. This could be Object, or Void, or another specific classe. Think about what you want to do with it, because the association is arbitrary...