Review array creation, defensive copying, constructor parameters, static imports, and runtime array-store checks.
Answer: Yes. It allocates an outer array of length three whose elements are initially null references to int arrays. Rows can later be assigned different lengths.
new int[][] alone is invalid because it provides neither a dimension expression nor an initializer. However, new int[][] {} is a valid empty initializer. Merely declaring int[][] grid; needs no size.
Answer: Declare two array parameters and decide whether to retain or copy the supplied arrays. Defensive copying is useful when later caller mutation must not change the object's state.
import java.util.Arrays;
import java.util.Objects;
public final class ArrayPair {
private final int[] first;
private final int[] second;
public ArrayPair(int[] first, int[] second) {
this.first = Objects.requireNonNull(first).clone();
this.second = Objects.requireNonNull(second).clone();
}
public int firstValue() { return first[0]; }
public String secondValues() { return Arrays.toString(second); }
public static void main(String[] args) {
int[] input = {2, 3};
ArrayPair pair = new ArrayPair(input, new int[] {5, 7});
input[0] = 99;
System.out.println(pair.firstValue());
System.out.println(pair.secondValues());
}
}
The output is 2 and [5, 7]. The example accepts empty arrays, but firstValue requires a nonempty first array; a production API should document or validate that requirement. Cloning these primitive arrays copies their values.
Answer: Yes. Arrays are reference types and can be ordinary constructor parameters. Java passes their reference values by value, not the arrays by reference in the parameter-passing sense.
Retaining an input reference shares the array, so mutations can affect both caller and object. Validate lengths when parallel arrays must align. A reference-array clone copies references rather than deeply copying element objects, and a multidimensional array clone copies only the outer array.
Answer: They are expressions supplied between the invocation's parentheses. Parameters are the receiving variables in the declaration. Argument expressions are evaluated from left to right and their values are passed to parameters.
An array creation expression can itself be an argument, such as new int[] {2, 3}. No named temporary variable is required.
Answer: It allows accessible static members of a type to be referred to by simple name, for example import static java.lang.Math.max;. It changes name resolution, not access permissions.
A static import does not copy a method into the importing class or make it an instance method. Excessive wildcard imports can create ambiguity; use qualification when it communicates the member's origin more clearly.
Answer: Each outer element is a reference to an int array, or null. For example, int[][] primes = {{2, 3, 5}, {7, 11, 13}, {17, 19, 23}}; creates an outer array and three row arrays.
The value 1 is not prime, so it should not start this example's prime sequence. Java's arrays of arrays can be jagged; row lengths need not match, and rows can even share the same array reference.
Answer: Use an array creation expression directly where its value is needed, such as Arrays.toString(new int[] {11, 23, 31}). This is often called an anonymous array, although it is an ordinary array object.
For nested arrays, use new int[][] {{2, 3}, {5, 7}}. The initializer supplies the lengths; do not combine explicit lengths with that initializer syntax.
Answer: Write scores = new int[] {4, 7, 2};. The shorter brace-only form is permitted in a declaration's array initializer, not as a standalone right-hand expression in an ordinary later assignment.
The assignment replaces the reference stored in scores. Other aliases, if any, still refer to the previous array. It does not resize or rewrite that old array.
Answer: Int values and values of byte, short, or char can be assigned through identity or widening conversion. Long, float, and double require an appropriate explicit narrowing conversion. Boolean is incompatible.
Once assigned, the element stores an int value. The array does not remember whether it originated from a byte or char variable. An Integer can also be unboxed when non-null, but that is a wrapper conversion rather than another primitive element type.
Answer: The assignment must satisfy compile-time rules, and a non-null value must also be compatible with the actual array's runtime component type. Reference-array covariance can make this runtime check necessary.
public class ArrayStoreRules {
public static void main(String[] args) {
Object[] values = new String[1];
values[0] = "Java";
System.out.println(values[0]);
try {
values[0] = Integer.valueOf(25);
} catch (ArrayStoreException expected) {
System.out.println("Incompatible element");
}
}
}
This prints Java and Incompatible element. Null is permitted in a reference array. Primitive arrays do not widen as whole arrays: an int[] cannot be assigned to a long[] variable even though individual int values can widen to long.
References: JLS: arrays, JLS: conversions, and JLS: imports.