I found myself checking for this and asking if it's necessary. I have code like this:
public Object myMethod(Object... many) {
if (many == null || many.length == 0)
return this;
for (Object one : many)
doSomethingWith(one);
return that;
}
But then I wondered... Am I being too cautious? Do I have to check if many == null
? Is that ever possible in any current Java version? If so, how? If not, I'll probably keep checking, just for futureproofing in case Oracle decides it can be null one day.
myMethod(null)
not pass it an array likenew Object[]{null}
?null
can be explicitly passed to varargs methods with cast -(Object[]) null
, so yes, passingnull
is possible in Java and your check makes sense