Sunday, February 15, 2009

When to use an array of size zero ?

Question: Java provides the facility to have an array of size zero. Do we have any scenario when this zero size array can be used ?

Scenario1: Suppose, I have a method returning an array. So the method can return an array with values or null. If the code invoking method is not checking for null, can get an exception.
So sometimes it is better to return an array with size zero to avoid NullPointerException. It could be a good way to return nothing :)

Scenario 2: Think and put a comment please.....