Home > front end >  I declared ArrayList of String by generic, but filled it with Integer somehow. Did I break java?
I declared ArrayList of String by generic, but filled it with Integer somehow. Did I break java?

Time:11-18

I wanted to fill an ArrayList with [0..10] no matter if it is Integer or String passed, and unintentionally did this:

public class Main {
    public static void main(String[] args) {
        Task1<String> task1instance = new Task1<>();
        System.out.println(task1instance.test());
    }
}

public class Task1<T extends Serializable> {
ArrayList<T> array = new ArrayList<>(10);

private void fillArray() { //String and Integer autofill supported
    for (Integer i = 0; i < 10; i  ) {
        array.add((T) i); //magic
    }
}

ArrayList<T> test () {
    fillArray();
    //smartSwap();
    System.out.println(this.array.get(0).getClass());
    return this.array;
}

I have output like:

class java.lang.Integer
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

But in the main I passed a

< String >

An ArrayList of String was filled with Integer, but if we try to cast it directly, then we have a compilation error:

Integer a = 9;
String b = (String) a; //error: Inconvertible types; cannot cast 'java.lang.Integer' to 'java.lang.String'

Tell me please, what actually happened?

CodePudding user response:

No, you won't get an exception. What you got was an "unchecked cast" warning from the compiler on the line array.add((T) i); //magic, which was telling you about this exact issue.

  • Related