0

I have one Java class with generics implementation. If I understood properly due to type erasure I can run that class file in java 1.4. I try to find out 1.4 compiler but not able to find out.

In nutshell , as per my understanding due to type erasure after compilation there will be no difference between class file complied in 1.4 and 1.5.

Any one have idea , please let me know my understanding is correct or not...

Sujoy
  • 117
  • 2
  • 9
  • generics were added to JDK 5. If you plan to use them in JDK 4 you'll have to use some library. A better suggestion would be to not use either 4 or 5. The current version is JDK 8. You ought to upgrade will all possible speed. – duffymo Aug 25 '16 at 15:37
  • @duffymo But isn't the question about whether a _class_ file from Java 5, having had its types erased, be able to run on a Java 4 JVM? – Tim Biegeleisen Aug 25 '16 at 15:40
  • 2
    You can specify the compiler's `-source` and `-target` if you wish – Hovercraft Full Of Eels Aug 25 '16 at 15:40
  • JDK 4 didn't have generics. If you write code that uses them, I doubt that JKD 4 will compile or run. To me it's a moot point: Nobody should be running on JDK 4 or 5. That's 3-4 version behind, almost a decade. – duffymo Aug 25 '16 at 15:42

2 Answers2

1

Yes. There won`t be any difference in class file in the context of Generics. Compiler takes care of generics. Once you have a class file that means compiler has already done its job by converting types.

Darpan27
  • 239
  • 1
  • 4
  • 9
  • The class files are different versions. A J2SDK 1.4 VM will not load a class file produced by the Java 5 compiler from a source containing generic types. – davmac Aug 25 '16 at 17:17
1

No. The Java 5 compiler produces a newer class file format which the Java 1.4 VMs can not read.

Type erasure means that the operation codes (within the bytecode itself) may be identical between legacy code and "generified" code, but there are other differences in the class file format (and there might be some new opcodes).

Java 5 code is compiled to a class file with version 49; Java 1.4 targetted code compiles to a class file with version 48. Version 49 class files support annotations, enums, generic type signature attributes, "enclosing method" attribute, varargs and bridge attributes, etc. An annotated chapter from the virtual machine specification showing the changes can be found here.

You can of course use the Java 5 compiler to compile Java 1.4 code and produce the older class file format, using the -source 1.4 -target 1.4 command line options.

davmac
  • 20,150
  • 1
  • 40
  • 68