casting to generic array in ObjectArrayFIFOQueue

8 views
Skip to first unread message

Péter Zarándy

unread,
May 4, 2021, 3:34:41 AM5/4/21
to fastutil

I've been using fastutil for some time and the other day I wanted make some changes in the implementation of ObjectArrayFIFOQueue, and I realized that one of the lines in the constuctor ObjectArrayFIFOQueue(final int capacity) is casting Object[] to K[].

I was under the impression that this is not possible in Java, and when I actually tried to extend the class and make my changes, when I instantiated this subclass I got a ClassCastException, which makes sense to me, but I don't understand why isn't the exception is thrown when I'm using simply ObjectArrayFIFOQueue.

Can you pls enlighten me? Is something happening in the background I'm not aware of?

Thanks!

Sebastiano Vigna

unread,
May 4, 2021, 4:06:37 AM5/4/21
to fastutil
Well, there's type erasure, so your array is an array of objects in any case. I don't know what you are doing exactly, so it's difficult to answer...

seba 
Reply all
Reply to author
Forward
0 new messages