Java Questions 41 - 50  «Prev  Next»

Java SE 25 Interview Questions: Shadowing and Array Allocation

Distinguish shadowed names, array reference declarations, element defaults, and the JVM model of object allocation.

  1. What is variable shadowing?

    Answer: A declaration can make another same-named declaration unavailable through a simple name in part of its scope. A constructor parameter named value can shadow a field, with this.value = value; distinguishing the field from the parameter.

    Not every redeclaration is legal. Java generally prohibits declaring another local with the same name in an overlapping local scope. Shadowing should not be confused with overriding methods or with assigning a new value to an existing variable.

  2. Which element types can an array have?

    Answer: An array has a component type that is primitive or reference. Examples include int[], String[], and int[][]. The last is an array whose elements are references to int arrays.

    The array itself is an object even when its elements are primitive values. All its elements obey the component type's assignment rules; an int array does not dynamically change into an array of another primitive type.

  3. Where is an array allocated?

    Answer: In the JVM's abstract runtime model, arrays are objects allocated from the heap. The variable holding the array reference may be a field, local, parameter, or array element.

    Do not infer a guaranteed physical location for every optimized program. A JVM may eliminate or transform allocations while preserving the behavior required by the language and runtime specifications.

  4. How do primitive-array and reference-array declarations differ?

    Answer: The component type differs: int[] keys; declares a reference to an array of int values, while Thread[] threads; declares a reference to an array of Thread references.

    Both variables have reference types, and neither declaration alone creates an array. Using brackets after the type is the clearer common convention, although Java also permits brackets after an individual variable name.

  5. Can an array size appear in the variable's declared type?

    Answer: No. int[4] values; is illegal. The size belongs in an allocation expression, as in int[] values = new int[4];, or follows from an array initializer.

    Length is not part of the array type: two int arrays of different lengths still have the same array type. Each allocated array's length is fixed.

  6. Does declaring an array variable allocate element storage?

    Answer: No. Declaring the reference does not allocate an array object. The program needs an allocation, an initializer, or an existing array reference supplied by some other expression.

    new int[0] still creates a zero-length array object. A negative requested length throws NegativeArraySizeException, while an unassigned local reference cannot be read in the first place.

  7. Do all Java objects belong to the heap model?

    Answer: The JVM specification models class instances and arrays as allocated from the heap. Stack frames hold execution data such as locals and operand stacks, which may contain references to those objects.

    This is a semantic model, not a promise that each source-level object must survive as a distinct physical heap allocation after optimization. Application code should depend on behavior, not on guessed object addresses.

  8. How can declaration and array creation be separated or combined?

    Answer: They can be separate statements, int[] scores; scores = new int[4];, or combined as int[] scores = new int[4];. An initializer such as int[] scores = {2, 3, 5, 7}; supplies explicit values.

    A local reference must be assigned before reading it. Assigning a different array later changes the reference, not the length of the original array.

  9. What does new Thread[5] create?

    Answer: It creates one array object containing five null reference elements. It does not construct five Thread objects and does not start any threads.

    public class ArrayObjects {
        public static void main(String[] args) {
            Thread[] threads = new Thread[5];
            System.out.println(threads.length);
            System.out.println(threads[0] == null);
            int[] scores;
            scores = new int[] {2, 3, 5, 7};
            System.out.println(scores[2]);
        }
    }

    The output is 5, true, and 5. Any required Thread instances must be supplied separately; an array is simply storage for their references.

  10. Are create, construct, and instantiate interchangeable?

    Answer: They are often used informally for bringing an object into existence. In a precise explanation, distinguish allocation, constructor execution, and initialization.

    For example, an array is an object but does not run an ordinary class constructor. Creating a reference variable alone does not instantiate the object it might later reference.

References: JLS: shadowing and scope, JLS: arrays, and JVMS: runtime data areas.