gpt4 book ai didi

java - 为什么自动装箱在 eclipse 中有效而在 javac 中无效?

转载 作者:太空狗 更新时间:2023-10-29 22:35:17 25 4
gpt4 key购买 nike

这段代码:

Integer ints[] = new Integer[]{'1', '2', '3'};

在 eclipse 中编译得很好,但是 javac(版本 1.6.0_27 和 1.7.0)给出了以下错误:

BoxTest.java:4: incompatible types
found : char
required: java.lang.Integer
Integer ints[] = new Integer[]{'1', '2', '3'};

BoxTest.java:4: incompatible types

为什么?

我假设它是某种编译器标志,但通过 eclipse 挖掘来找出它并不完全直接。

最佳答案

javac 没有做的,不是自动装箱,而是自动转换。在 javac 中,它编译为:

Integer ints[] = new Integer[] { (int) '1', (int) '2', (int) '3' };

只有一个 Integer 也会发生同样的情况,同样在 javac 中,我必须进行显式转换才能编译:

Integer a = (int) '1';

但这是我发现的。使用Eclipse JDT batch compiler从命令行它可以工作,即使没有强制转换:

$ java -jar org.eclipse.jdt.core_3.7.1.v_B76_R37x.jar -classpath rt.jar \
-1.6 Appo.java

我看过 options of javac而且我认为没有任何方法可以改变这种行为。

我不得不推断,差异是由于 Eclipse 在内部使用的不是 javac,而是 JDT 编译器

关于java - 为什么自动装箱在 eclipse 中有效而在 javac 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7918134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com