Java 10编译的类可以在9上运行吗? [英] Can Java 10 compiled classes run on 9?

查看:105
本文介绍了Java 10编译的类可以在9上运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确,Java 10的二进制形式与Java 9完全相同,那么就不会出现任何问题。

If I understand correctly the binary form of Java 10 is 100% identical to Java 9 so it shouldn't make any problems.

我的假设是否正确?

推荐答案

这与Java 10无关;规则与以往一样。

This has nothing to do with Java 10; the rules are the same as they've always been.

在每个Java版本中,类文件版本号都已递增。您可以在较新的Java运行时上运行较旧的类文件(您可以运行二十年前在Java 10上的Java 1.0下编译的文件!),但是从未运行 newer 旧运行时的类文件。这在Java 10中没有改变。

In every version of Java, the classfile version number has been incremented. You can run older classfiles on a newer Java runtime (you can run files compiled twenty years ago under Java 1.0 on Java 10!), but it has never been true that you can run newer classfiles on an older runtime. This hasn't changed with Java 10.

但是,如果您不使用自该版本以来引入的更新功能,则可以编译类以使它们在旧版本上运行。以前这是通过 -source -target 开关完成的:

However, you can compile classes such that they run on older versions, if you don't use newer features introduced since that version. This used to be accomplished by the -source and -target switches:

// Run this on Java 8
javac -source 6 -target 6 Foo.java

您将获得将在Java 6及更高版本上运行的类文件。 (源版本不能大于目标版本。)最近用更全面的 - 发布选项取代:

and you'll get classfiles that will run on Java 6 and later. (The source version cannot be greater than the target version.) This was recently replaced with the more comprehensive --release option:

javac --release 9

这意味着没有只有 -source 9 -target 9 ,但编译器也会阻止你使用9之后引入的JDK功能。

which implies not only -source 9 and -target 9, but the compiler will also prevent you from using JDK functionality introduced after 9.

这篇关于Java 10编译的类可以在9上运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆