Are You a Generics Rock Star or Just Generic?

October 10th, 2012 @   - 

It’s been eight full years since Sun, now Oracle, released Java 5. At the time, Java was under heavy fire from open source and mainstream languages such as .Net and attacked for its complexity and bloat – especially with its enterprise edition. Sun responded by introducing annotations, auto boxing, enums, var args, enhanced for loops and generics. Eight years later, practically a generation in software development years, generics remain the least understood feature of that release because, ironically, they are highly complex! While generics is a first-class language construct and should be mastered by all Java practitioners, most of us prefer to remain in the shallow end of the pool.

Are generics truly complex? Absolutely. Even IntelliJ’s syntax checker gets it wrong sometimes. Generics ensure compile-time type safety and pass on the complexity to you the developer. That’s the price of admission into Java’s strongly-typed world. But what else did you expect? Java is not PHP.

So how deep is your knowledge of generics? Are you a generics rock star or just a generic developer hacking your way through? Find out by taking the Java generics test.  (And yes, the test counts – enjoy!)

The Java Generics Test

Instructions: For each question, simply answer legal if you think the code snippet compiles or illegal otherwise. You may consult the javadoc. Assume the language level is Java 7.

1)      List<String> list = new ArrayList<String>();

2)      List<String> list = new ArrayList<>();

3)      List<Object> list = new ArrayList<String>();

4)      ArrayList<Object> list = new ArrayList<String>();

5)      ArrayList<String> list = new ArrayList<Object>();

6)      ArrayList list = new ArrayList<String>();

7)      ArrayList<String> list = new ArrayList();

8)      ArrayList<String> stringList = null;

ArrayList<? extends String> extendsStringList = stringList;

9)     ArrayList<?> list = new ArrayList<Number>();

10)    ArrayList<? extends Number> extendsList = null;

ArrayList<? super Number> superList = extendsList;

11)   ArrayList<? super Number> superNumList = null;

ArrayList<? super Integer> superIntList = superNumList;

12)   ArrayList<Object> list = new ArrayList<Object>();


13)   ArrayList<?> list = new ArrayList<String>();


14)   ArrayList<? super Integer> list= new ArrayList<Integer>();


15)   ArrayList<? extends Number> list= new ArrayList<Integer>();

list.addAll(new ArrayList<Integer>());

16)   ArrayList<? super Integer> superIntList = new ArrayList<Number>();

ArrayList<? extends Integer> extendsIntList = new ArrayList<Integer>();


17)   ArrayList<Integer> intList = new ArrayList<Integer>();

intList.add(new Integer(1));

ArrayList<? extends Number> extendsNumList= intList;

Integer integer = extendsNumList.get(0);

18)   ArrayList<String>[] twoDimArray = new ArrayList <String>[10];

19)   Pair<String, String>[] array = new Pair[2];

array[0] = new Pair<String, String>(“1”, “y”);

array[1] = new Pair(true, false);

20)  Pair<?,?>[] array = new Pair<?,?>[2] ;

array[0] = new Pair<Integer,Integer>(0,0);

array[1] = new Pair<String,String>(“Hello”,”There”);

See answers here.

Comments are closed.