liveBook · Manning

Array

As explained in 100 Java Mistakes and How to Avoid Them

An array is an indexed Java object or primitive container with a fixed length and a single component type.

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

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains compile-time wrapping and equality behavior with focused examples, but stops short of general array implementation internals.
Examples
Uses printAll calls, a generic contains helper, array equality, and explicit null casts as diagnostic examples.
What it assumes
Assumes familiarity with Java method calls, declared types, generics, collections, and Object methods.
What you can do after
Diagnose ambiguous calls, choose explicit casts, preserve array-versus-collection semantics, and compare array contents correctly.
How it connects
Connects arrays to variable-arity methods, collections, generics, null, object identity, and content equality.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.