0

While going through some questions on Generics, I came accross the question-

Why can't we have Generic Arrays in Java?

Next, I explored and came accross following article- https://www.baeldung.com/java-generic-array
Now, its mentioned in the article-

An important difference between arrays and generics is how they enforce type checking. Specifically, arrays store and check type information at runtime. Generics, however, check for type errors at compile-time and don't have type information at runtime.

I am not able to properly understand why we can't specify generics for array. Now, I wrote a small program on my own-

package Generics;

import java.util.Arrays;

public class TypeErasure {
    private static class GenericClass<T> {
        T[] arr; // no error

        GenericClass(T[] arr) {
            this.arr = arr; // no error
        }

        void display() {
            System.out.println(Arrays.toString(arr));
        }

        // https://www.geeksforgeeks.org/generics-in-java/
        public T[] getArray(int size) {
            // T[] genericArray = new T[size]; //compile time error
            // return genericArray;
            return null;
        }

        public T[] getArray() {
            return arr; // error- Type mismatch: cannot convert from T[] to T[]
            // return null;
        }
    }

    public static void main(String[] args) {
        GenericClass<Integer> gc = new GenericClass<>(new Integer[] { 1, 2, 3 });
        gc.display();
        gc.getArray(5);
        gc.getArray();
    }
}

Following is the de-compiled .class file-

package Generics;

import java.util.Arrays;

public class TypeErasure {
    public TypeErasure() {
    }

    public static void main(String[] var0) {
        GenericClass var1 = new GenericClass(new Integer[]{1, 2, 3});
        var1.display();
        var1.getArray(5);
        var1.getArray();
    }

    private static class GenericClass<T> {
        T[] arr;

        GenericClass(T[] var1) {
            this.arr = var1;
        }

        void display() {
            System.out.println(Arrays.toString(this.arr));
        }

        public T[] getArray(int var1) {
            return null;
        }

        public T[] getArray() {
            return this.arr;
        }
    }
}

My question-
what problem can occur by making the generic array assignment in the example I have provided? I need to understand the difference between runtime checking and compile time checking and why arrays need to do runtime checking?

Can somebody help me understand why the code commented out in getArray() method gives compile time error?

ayush
  • 464
  • 5
  • 17
  • 1
    You re-define the generic type `T` on the method. Use `public T[] getArray() { ... }` instead. --- `... new T[size];` due to type erasure, this cannot work. --- In general, mixing generics with arrays is a recipe for trouble. Not only are generics erased, while arrays are retained, generics are also invariant while arrays are covariant. – Turing85 Jul 31 '23 at 10:56
  • ou're asking two different questions: Q1: Why does `public T[] getArray()` give an error (e.g. `error: generic array creation`)? Q2: why can't you create generic arrays in Java? As the Baeldung article says, Java arrays do runtime type checking; Java generics don't. Generics do compile time checking: they exhibit [type erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html), and incur no runtime overhead. Consequently, adding type erasure to Java arrays would BREAK "backward-compatibility". – paulsm4 Jul 31 '23 at 19:45
  • Thanks for pointing it out. This is the point which I want to understand with an example. I mean what problem can occur by making the generic array assignment in the example I have provided? I need to understand the difference between runtime checking and compile time checking and why arrays need to do runtime checking? – ayush Aug 01 '23 at 01:28
  • @tgdavies the answer you provided gives a method to create a generic type of array. Its of great help but does not entirely address what I want to ask. Thanks! – ayush Aug 01 '23 at 01:29

0 Answers0