【发布时间】:2015-05-05 18:28:53
【问题描述】:
我在下面有一个相当简单的测试用例。我可以毫无问题或错误地将空值发送到构造函数,但是当我尝试将空值发送到方法时,它会出错:error: incompatible types: <null> cannot be converted to int(或任何预期的类型)。我不确定为什么会发生这种情况,而且我在很多地方都看到发送空值是可以接受的做法。实际上,我只需要空值,以便我可以将此示例泵入 Soot 和 Spark 进行静态分析,因此发送到方法的实际参数与 Spark 静态分析中入口点的语义必要性无关。
public class Test {
public Test(Object var1, Object var2) {
//do irrelevant stuff here with var1 and var2
}
public void api1(int x, int y) {
// do irrelevant stuff with x and y
}
public List<String> api2(String x, int y, boolean a) {
// do irrelevant stuff with x, y, and a and return a new ArrayList<String>()
}
}
public class Main {
public static void main(String[] args) {
Test usingVars = new Test(1, 2); // works, compiles and no errors
Test usingNulls = new Test(null, null); // works, compiles and no errors
/**
* usingVars will obviously work and not complain at all
*/
usingVars.api1(1, 2); // works, compiles and no errors
usingVars.api2("test", 1, false); // works, compiles and no errors
/**
* usingNulls should work, but throws this error on compilation:
* error: incompatible types: <null> cannot be converted to int
*/
usingNulls.api1(null, null); // should work, doesn't compile errors out
usingNulls.api2(null, null, null); // should work, doesn't compile errors out
}
}
【问题讨论】:
-
null与int的类型不同。int之类的原语不能是null。 -
对于您的
Test(null, null),您必须有第二个接受对象的双参数构造函数。正如其他人所说,原语不能设置为空。如果要将 int 设置为默认状态,请将其值设为 0 并检查。 -
我不确定您是否使用了您认为与
new Test( null, null)一起使用的构造函数。 int 是原始类型,不能设置为 null。看看是否有另一个构造函数接受某种对象作为参数;那就是你正在使用的那个。 -
我编辑了代码以更恰当地反映我正在使用的 API。它们在构造函数中有可为空的对象。
-
我相信您通过将 int 作为对象传递所做的事情是在堆上创建给定 int 内容的盒装副本。这可能不是您想要实现的目标。如果你需要一个构造函数有两个整数,最好是具体的,否则你可能会遇到意想不到的结果。
标签: java methods null parameter-passing static-analysis