what's the difference between -source and -target compatibility?

怎甘沉沦 提交于 2019-11-28 05:43:31

From the javac docs:

-source Specifies the version of source code accepted.

-target Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM.

In your example:

-source 1.5 and -target 1.6

This would be used to make sure that the source code is compatible with JDK 1.5, but should generate class files for use on JDK 1.6 and later.

Quite why you would do this is another matter.

The -source indicates what level of compliance your source code has: are you using Annotations? Then you would need at least 1.5; are you using @override on interface implementations, you would need 1.6 etc

The -target specifies what Java version you want to be able to run your classes on. You could use a Java SE 7 compiler and compile to run on Java SE 1.5.

This is mostly useful to produce a jar file working with an older version of Java. I believe that so far all JDKs are able to execute older version too, so there is no real reason to have target larger than source.

It does however make sense to set target to e.g. 1.6 when using a 1.7 JDK.

I'm not sure, but I believe it could work in some situations to compile a 1.7 java code using a 1.7 compiler to a 1.6 jar, for example expressions such as

ArrayList<Integer> foo = new ArrayList<>();

that are only valid in 1.7+ source version should compile to 1.6 compatible byte code. But I have not verified whether the compiler will actually do this. Unfortunately, this doesn't seem to be implemented in practise.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!