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?
Integerbecause that's what you put in your array. TrySystem.out.println(task1instance.test().get(0));in your main method and you'll get a ClassCastException. It's just not breaking insideTask1because of type erasure.for (Integer i = 0; i < 10; i++) { array.add((Object) i); }, which is perfectly legal. The casting to the generic type doesn't occur until you try to access the data outside the generic class. After my afternoon spate of meetings, if nobody else has answered yet, I'll come back and write up a proper explanation.