Skip to content

Instantly share code, notes, and snippets.

@noelmarkham
Created March 16, 2013 14:53
Show Gist options
  • Save noelmarkham/5176718 to your computer and use it in GitHub Desktop.
Save noelmarkham/5176718 to your computer and use it in GitHub Desktop.
Testing variance in Java's generics. Why on earth doesn't this compile? In IntelliJ, getClass on line 13 errors as 'cannot resolve symbol' Javac doesn't produce a logical error.
public class GenericVariance {
class A {}
class B extends A {}
@Test
public void checkTypes() {
assertTrue(new B() instanceof A);
final List<A> listOfA = new ArrayList<>();
final List<B> listOfB = new ArrayList<>();
assertFalse(listOfB instanceof listOfA.getClass());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment