An array is an indexed Java container with a fixed length and one component type. Its behavior becomes easy to misread when arrays meet variable-arity methods, collections, or ordinary object comparison. section 2.9.1 section 7.2
Watch for these array-related mistakes:
- A variable-arity method accepts either separate arguments or an explicit array, but a single argument is classified from its declared type. An Object variable containing an Object[] is wrapped again. section 2.9.1
- Passing null to an Object... parameter selects the array interpretation because null can be assigned to an array type, leaving the parameter null and potentially causing NullPointerException. section 2.9.1
- Passing a collection where an array is expected can compile by wrapping the collection as one element, changing a contains check into a call that always returns false. section 2.9.2
- Use Arrays.equals() or Arrays.deepEquals() for array contents; array.equals() otherwise retains Object’s identity behavior. section 7.2
Disambiguating null
printAll((Object)null) requests an array containing one null element; printAll((Object[])null) passes a null array reference. The cast makes the intended call explicit. section 2.9.1
Object obj = new Object[]{"Hello", "World"};
printAll(obj);
Inspect how an Object variable holding an object array is wrapped into another array during a variable-arity call. section 2.9.1
// I want an array containing null printAll((Object)null); // I want just null array reference printAll((Object[])null);
Compare explicit casts that distinguish one null element from a null array reference. section 2.9.1
static final List<String> allLanguages =
List.of("Java", "Groovy", "Scala", "Kotlin");
static boolean isLanguage(String language) {
return contains(language, allLanguages);
}
Trace how changing an array variable to a List can preserve compilation while changing the meaning of a generic vararg call. section 2.9.2







































