if99.net

IF99 ITB

Archive for the ‘generic’ Category

Little Java Generic Quiz

without comments

Let say I have following code:

interface TestGeneric {

   public Integer test(List<Integer> a);
   public Boolean test(List<Boolean> a);

}

Can you guess what are the answers for following questions?

  • TestGeneric will be reported as error by Java 6 (true/false)
  • TestGeneric will be reported as error by Java 7 (true/false)
  • TestGeneric will be reported as error by Eclipse 3.5 using Java 6 (true/false)
  • TestGeneric will be reported as error by Eclipse 3.5 using Java 7 (true/false)
  • TestGeneric will be reported as error by (upcoming) Eclipse 3.6 using Java 6 (true/false)
  • TestGeneric will be reported as error by (upcoming) Eclipse 3.6 using Java 7 (true/false)

What’s your guess?

The correct answer is (highlight to see the answer): false, true, false, (can Eclipse 3.5 run Java 7?)?, true, true.

The fact that statement 1 is false is related to this bug entry: http://bugs.sun.com/view_bug.do?bug_id=6182950

Eclipse (as 3.6M2) offered a more consistent behavior by considering all cases as compile error as discussed here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=289247

And this op4j used exactly this feature and will not be compiled under Java 7 or using Eclipse 3.6.

Related posts:

  1. Java Tips: Using generic correctly
  2. Be extremely careful when you initialize a non-final field in the declaration!
  3. Java Tips: Memory Optimization for String

Little Java Generic Quiz originally appeared on satukubik on April 16, 2010.

Written by Nanda Firdausi

April 16th, 2010 at 7:54 pm