2

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?

4
  • 1
    You're seeing Integer because that's what you put in your array. Try System.out.println(task1instance.test().get(0)); in your main method and you'll get a ClassCastException. It's just not breaking inside Task1 because of type erasure. Commented Nov 17, 2021 at 18:27
  • @azurefrog but how did compiler allow this? Where did I confused the compiler and let this happened?)) Commented Nov 17, 2021 at 18:35
  • Common sense tells me that I must have got an exception when I tried putting Integer there, but not Commented Nov 17, 2021 at 18:38
  • 2
    Basically, at compile time, what you're doing is 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. Commented Nov 17, 2021 at 18:38

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.