0

Why does the safety of the cast in makeArray bank on whether user gets access to the array created?

@SuppressWarnings("unchecked")
private E[] makeArray(int s) {
    return (E[]) new Object[s];
}

I'm guessing that the user might be able to use the wrong data type if they get a hold of the array. For example, instead of using s type elements, the user instead uses T type elements.

buræquete
  • 12,943
  • 4
  • 34
  • 69
Kazou1388
  • 15
  • 2
  • Because the user may store an object that is not of type `E` into the returned array and this will throw `ArrayStoreException` only at **runtime** – Meme Composer Dec 18 '17 at 04:04
  • 2
    `new Object[s]` does not create an array of `s` type elements. `new Object[s]` creates an array of `Object` **of size `s`** – Bohemian Dec 18 '17 at 04:14

0 Answers0