Skip to content

Tag: generics

Getting the class of the components of an array

If I have how can I get T.class from array? If I do array.getClass() that gets me T[].class instead. Answer Component Type Use this: Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null. Reference: Class.getComponentType() Safe…

Why doesn’t this generic cast fail?

I’d expect this code to throw a ClassCastException: But it doesn’t. Casting String to T doesn’t fail, until I use the returned object somehow, like: Background: I created a Class which uses JAXB to unmarshal an XML file. It looks like this: Depending on whether the root-Element is an anonymo…

Java generics super keyword

I went through these topics Generics..? Super T Bounding generics with ‘super’ keyword However, I still seem to be kind of lost with super keyword: When we declare a collection like that: shouldn’t it be the opposite – we have a list that contains some objects (of unknown type) which a…

java generics covariance

I am having trouble understanding the following article: http://www.ibm.com/developerworks/java/library/j-jtp01255.html Under, Generics are not covariant the author states, Because ln is a List, adding a Float to it seems perfectly legal. But if ln were aliased with li, then it would break the type-safety pro…