在Java中有没有一种方法来确定自动装箱是否已经完成?
例如
void functionInt(Integer i) {
//Determine if Integer was passed or int was passed. Is it possible?
}
int i = 1;
int ii = new Integer(1);
functionInt(i);
functionInt(ii);被呼叫者能区分这两个呼叫吗?
发布于 2012-07-15 08:18:36
functionInt将始终传递一个Integer,并且无法确定该Integer是否是自动装箱的结果。
您可以做的是创建重载函数:
void functionInt(Integer i);
void functionInt(int i);/edit
如果您有以下类:
public class Foo {
public Foo(int primitive) {
System.out.println("Created!");
}
}您可以毫无问题地调用Foo.class.getConstructor(int.class).newInstance(new Integer(5))。如果找不到参数为java.util.Integer的Constructor,那么检查参数为int.class而不是java.util.Integer应该是相当简单的。
https://stackoverflow.com/questions/11488400
复制相似问题