javac source and target options usage

不羁岁月 提交于 2019-12-25 03:52:28

问题


When we use javac option:
-source and -target
how does the compiler "knows" the code to produce ?

I mean how does any compiler knows the specification of any previous java release or I have to do some setting in order to provide it with some information about them ?


回答1:


The javac program knows how to handle these arguments without you providing any extra information. Do note that there is a bit of a trap here, however: these options relate to the source syntax and the class file form and content, but they do not take into account the Java standard library. As a result, they will be perfectly content to compile your code that relies on standard library classes or methods that are new in (say) Java 7, despite the source or target Java version being set to something earlier. In principle the result could run on that earlier Java version, but in practice that would require the later standard library to be backported, at least in part.

If you are developing for a particular minimum version of Java, you should develop using that version. The -source and -target options options come into play if you or someone else later wants to rebuild the source using a later javac, without dropping runtime compatibility with the earlier target Java.



来源:https://stackoverflow.com/questions/32851611/javac-source-and-target-options-usage

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