java -source 和 -target 兼容性之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10663450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:00:02  来源:igfitidea点击:

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

javacompilationjavacbackwards-compatibility

提问by Adam Lee

When using the Java compiler (javac), we can specify two kinds of compatibility. One is using -sourceand the other is using -target. What is the difference between these two?

在使用 Java 编译器 ( javac) 时,我们可以指定两种兼容性。一种是使用-source,另一种是使用-target。这两者有什么区别?

For example, -source 1.5and -target 1.6?

例如,-source 1.5-target 1.6

Also, is there any case where we use a different source and target compatibility level?

另外,有没有我们使用不同的源和目标兼容级别的情况?

采纳答案by skaffman

From the javac docs:

javac 文档

-sourceSpecifies the version of source code accepted.

-targetGenerate 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.

-source指定接受的源代码版本。

-target生成以指定版本的 VM 为目标的类文件。类文件将在指定的目标和更高版本上运行,但不会在 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.

这将用于确保源代码与 JDK 1.5 兼容,但应生成用于 JDK 1.6 及更高版本的类文件。

Quite whyyou would do this is another matter.

相当为什么你会做,这是另一回事。

回答by rapha?λ

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

-source指示什么级别遵守你的源代码有:您使用的注解?那么你至少需要1.5; 您是否@override在接口实现上使用,您需要1.6

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

-target指定了Java版本要能够在运行的类。您可以使用 Java SE7编译器并编译以在 Java SE 上运行1.5

回答by Has QUIT--Anony-Mousse

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.

这对于生成使用旧版本 Java 的 jar 文件非常有用。我相信到目前为止所有 JDK 也都能够执行旧版本,因此没有真正的理由让目标大于源。

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

然而,target在使用 1.7 JDK 时设置为例如 1.6 是有意义的。

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

我不确定,但我相信它可以在某些情况下使用 1.7 编译器将 1.7 java 代码编译为 1.6 jar,例如表达式,例如

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.

仅在 1.7+ 源版本中有效的应该编译为 1.6 兼容的字节码。但我还没有验证编译器是否真的会这样做。不幸的是,这似乎并没有在实践中实施。