-source 和 -target 兼容性之间有什么区别? [英] what's the difference between -source and -target compatibility?

查看:34
本文介绍了-source 和 -target 兼容性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如,-source 1.5-target 1.6?

此外,是否存在我们使用不同源和目标兼容级别的情况?

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

推荐答案

来自 javac 文档:

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

-source Specifies the version of source code accepted.

-target 生成以指定版本的 VM 为目标的类文件.类文件将在指定的目标和更高版本上运行,但不能在 VM 的早期版本上运行.

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

在你的例子中:

-source 1.5 and -target 1.6

这将用于确保源代码与 JDK 1.5 兼容,但应生成用于 JDK 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.

这篇关于-source 和 -target 兼容性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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