java 使用 JDK 7 与使用编译器合规性级别为 1.7 的 JDK 8 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27278494/
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-11-02 11:32:34  来源:igfitidea点击:

What is the difference between using JDK 7 vs using JDK 8 with compiler compliance level 1.7?

javaandroid

提问by Sudhir Khanger

I was wondering if there is any difference running/building a software under JDK 8 and using compiler compliance level 1.7 vs JDK 7 as system default? I am more interested in reference to Android building, building apps, Eclipse, Android Studio, etc.

我想知道在 JDK 8 下运行/构建软件和使用编译器合规性级别 1.7 与 JDK 7 作为系统默认值是否有任何区别?我对参考Android构建、构建应用程序、Eclipse、Android Studio等更感兴趣。

采纳答案by Dawood ibn Kareem

Yes, there are loads of new classes in the JDK 1.8, for example, the java.time classes. You won't get those if you build in JDK 1.7; but you will be able to use them if you build in JDK 1.8 with compiler compliance level 1.7.

是的,JDK 1.8 中有很多新类,例如 java.time 类。如果您在 JDK 1.7 中构建,您将不会得到这些;但是如果您在编译器合规级别为 1.7 的 JDK 1.8 中构建,您将能够使用它们。

回答by Pracede

Yes there is a difference between running/building a software under JDK 8 and using compiler compliance level 1.7 vs JDK 7 as system default.

是的,在 JDK 8 下运行/构建软件与使用编译器合规性级别 1.7 与 JDK 7 作为系统默认值之间存在差异。

  1. running a software under JDK 8 and using compiler compliance level : You compile in jdk 1.7 but run on 1.8. No problem, your programm will work as needed.

  2. JDK 7 as system default : You compile in 1.7 version and run on the same version.

  1. 在 JDK 8 下运行软件并使用编译器合规级别:您在 jdk 1.7 中编译但在 1.8 上运行。没问题,您的程序将根据需要运行。

  2. JDK 7 作为系统默认值:您在 1.7 版本中编译并在相同版本上运行。

I am wondering in waht case you would like to use the first case ?

我想知道在这种情况下您想使用第一种情况吗?

回答by Nazgul

truck load of difference actually. With JDKs the the compliance level is a directive to the compiler to specifically use the optimizations and linking features for the version you specified. It has a lot more going under the hood but I don't think you want to know that. New JDK versions bring new features and the compilers in those version are able to understand and link those features when building class files or assembled code of your source Java files. Consequently the JVM runtime in those JDKS is also equipped to handle such optimizations and cases and process them. So without compliance levels the class file that you build with JDK8 would only run correctly with JDK8 based runtimes. They may not do so with JDK7 or 6. To counter this problem and thus allow your JDK8 compiled code to run on JDK8,7 and maybe even on 6, hyou need to add compliance level to compiler directives accordingly. Downside is that you may not be able to use some latest features which the compiler offers but such cases are far few and outweigh the need for interoperability and potability.

实际货车载重不同。对于 JDK,合规性级别是编译器的指令,用于专门为您指定的版本使用优化和链接功能。引擎盖下还有很多事情要做,但我认为您不想知道这些。新的 JDK 版本带来了新特性,这些版本中的编译器在构建类文件或源 Java 文件的汇编代码时能够理解和链接这些特性。因此,这些 JDKS 中的 JVM 运行时也被配备来处理此类优化和案例并处理它们。因此,如果没有合规性级别,您使用 JDK8 构建的类文件只能在基于 JDK8 的运行时正确运行。他们可能不会在 JDK7 或 6 上这样做。为了解决这个问题,从而允许您的 JDK8 编译代码在 JDK8、7 甚至 6 上运行,你需要相应地向编译器指令添加合规性级别。缺点是您可能无法使用编译器提供的一些最新功能,但这种情况很少,并且超过了对互操作性和可移植性的需求。