Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 7 of 10 · Java Generics & Type Safety
Interview question

Why can't you create generic arrays in Java, and what are the workarounds? Java में generic arrays क्यों नहीं बना सकते, और workarounds क्या हैं?

Answer

Arrays in Java are covariant and retain their component type at runtime, but generics use type erasure - combining these would allow type-unsafe operations to slip past the compiler, so Java disallows creating generic arrays directly.

class Box<T> {
    // T[] items = new T[10];  // COMPILE ERROR: Cannot create a generic array
}

// Why this restriction exists - illustrating the danger it prevents:
Object[] objArray = new String[3];  // arrays ARE covariant, this compiles
objArray[0] = 42;  // would compile if T[] were allowed like this,
// but throws ArrayStoreException at runtime since the array is really a String[]
// Generics avoid this entire class of bugs by disallowing generic array creation

// WORKAROUND 1: use a List<T> instead of an array (most common solution)
class Box<T> {
    private List<T> items = new ArrayList<>();
    public void add(T item) { items.add(item); }
}

// WORKAROUND 2: create an Object[] internally, cast when accessing
// (unchecked but works because you control access carefully)
class GenericArrayBox<T> {
    private Object[] items;
    public GenericArrayBox(int size) {
        items = new Object[size];
    }
    @SuppressWarnings('unchecked')
    public T get(int index) {
        return (T) items[index];  // unchecked cast, but safe if used correctly
    }
    public void set(int index, T value) {
        items[index] = value;
    }
}

// WORKAROUND 3: pass a Class<T> token to create arrays reflectively
import java.lang.reflect.Array;
@SuppressWarnings('unchecked')
public static <T> T[] createArray(Class<T> type, int size) {
    return (T[]) Array.newInstance(type, size);
}
String[] strings = createArray(String.class, 5);

Java में arrays covariant होते हैं और runtime पर अपना component type रखते हैं, पर generics type erasure use करते हैं - दोनों को combine करना type-unsafe operations को compiler से बचने देता, इसलिए Java generic arrays directly बनाने नहीं देता।

class Box<T> {
    // T[] items = new T[10];  // COMPILE ERROR
}

// यह restriction क्यों है:
Object[] objArray = new String[3];  // arrays covariant हैं
objArray[0] = 42;  // compile होता अगर T[] allowed होता,
// पर runtime पर ArrayStoreException आता क्योंकि असल में String[] है

// WORKAROUND 1: array की जगह List<T> use करें
class Box<T> {
    private List<T> items = new ArrayList<>();
    public void add(T item) { items.add(item); }
}

// WORKAROUND 2: internally Object[] बनाएं, access पर cast करें
class GenericArrayBox<T> {
    private Object[] items;
    public GenericArrayBox(int size) {
        items = new Object[size];
    }
    @SuppressWarnings('unchecked')
    public T get(int index) {
        return (T) items[index];
    }
    public void set(int index, T value) {
        items[index] = value;
    }
}

// WORKAROUND 3: Class<T> token से reflectively array बनाएं
import java.lang.reflect.Array;
@SuppressWarnings('unchecked')
public static <T> T[] createArray(Class<T> type, int size) {
    return (T[]) Array.newInstance(type, size);
}
String[] strings = createArray(String.class, 5);

Was this answer clear?