0

recently I have been reading somewhat more about Java Generics and came to this article: http://gafter.blogspot.nl/2004/09/puzzling-through-erasure-answer.html, which basically says that Java will always be backwards compatible.

Now, what has Java done to JComboBox? Code written in Java 7, ie. JComboBox<String> comboBox = new JComboBox<>(); should compile just fine in Java 6, but then as a raw type as the type has been erased.

However as you can easily see when googling this, there are three different issues now:

  • First of all, Java 6 does not compile on Generics, while it should do so with type erasure and treat it as a raw type.
  • Secondly, not even reification is being used, so nothing is gained in Java 7.
  • And thirdly, Java 7 code that uses raw types gives warnings.

So there does not seem to be a correct way.

Regards.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
skiwi
  • 66,971
  • 31
  • 131
  • 216
  • 2
    Well, the diamond operator is introduced in Java 7, so I'm not sure why you say: it should compile just fine in Java 6. Cause it won't. – Erik Pragt Jul 23 '13 at 07:22
  • @ErikPragt I might have thought the other way around... So I thought the wrong way around? – skiwi Jul 23 '13 at 07:26
  • http://stackoverflow.com/a/4692743/604478. If you want compile for older version, you have to use the `-target` option. – Alberto Jul 23 '13 at 07:59

1 Answers1

1

You can specify a language target level and a bytecode target level. This way you can use all the language feature of a certain version and compile it against a specific vm version. I tested your example with the following setting: Language level 7, bytecode version 6 and it compiles just fine.

But you still would have to compile it against a jdk 7, so the compiler knows that the JCombobox is generic.

morpheus05
  • 4,772
  • 2
  • 32
  • 47