[英]Why doesn't the javac compiler flag this code with a warning about varargs ambiguity?
[英]Why javac “-source” flag doesn't work?
我正在测试javac -source
标志,我对它应该如何工作有点困惑。
请参阅此代码作为示例。 它是一个不兼容的Java5代码,因为在该版本的JDK中没有为String定义方法isEmpty()
。
public class TestJavac {
public static void Main(String args[]) {
String pippo = "pippo";
pippo.isEmpty();
}
}
尝试编译使用:
javac -source 5 TestJavac.java
然后它的作品! 这对我来说听起来很奇怪,但也许有些东西我无视。 我的JAVA_HOME
指向1.6 JDK
。
source选项仅适用于语言级别,不适用于API(由JDK运行时库提供)。
有一个单独的选项(-bootclasspath -extdirs)来指定要使用的JDK(并且需要获取相应的jar文件)。
请注意,还有-target(控制输出字节码版本)。
你应该总是指定所有(或者甚至使用较旧的JDK进行编译,你或多或少需要为第二个交换机工作)
源标志仅用于验证语言功能,来自-source
标志的文档:
1.3 The compiler does not support assertions, generics, or other
language features introduced after Java SE 1.3.
1.4 The compiler accepts code containing assertions, which were
introduced in Java SE 1.4.
1.5 The compiler accepts code containing generics and other language
features introduced in Java SE 5.
5 Synonym for 1.5.
1.6 No language changes were introduced in Java SE 6. However, encoding
errors in source files are now reported as errors instead of warnings
as in previous releases of Java SE.
6 Synonym for 1.6.
1.7 This is the default value. The compiler accepts code with features
introduced in Java SE 7.
如您所见,java 6中没有引入任何更改。
-source
仅检查java语法,而不是包含的库。 例如,它忽略了@since 1.6
:(
类似地, -target
将生成将加载到较旧版本的JVM但可能无法运行的类。
http://vanillajava.blogspot.co.uk/2012/02/using-java-7-to-target-much-older-jvms.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.