Fixing ugly Java APIs: read-only generic varargs - Christophe Beyls - Medium
To sum it up, as soon as you want to put generic types in a Java array, you're on your own to guarantee the safety of your code and to prevent a ClassCastException
from occurring at runtime. This is why collections should be preferred to arrays when dealing with generic types in Java. Changing the method signature to use a List<T>
instead of T...
would avoid compiler warnings and guarantee type safety at compile time:
Read full article from Fixing ugly Java APIs: read-only generic varargs - Christophe Beyls - Medium
No comments:
Post a Comment